Android download files, files including videos and various files

Source: Internet
Author: User
Tags sendmsg gtar

In the project to download the file and call the system software to open, the file is the server, the following are some code snippets:

Download files using the ProgressBar and HTTP protocols:

If you do not have this file, create and download it first, and if you have one, open it:

private void Playmeida () {


File2 = new File (Savepath + "/" + filename);
if (!file2.exists ()) {
New Thread () {
public void Run () {
try {
Down_file (path, savepath);


} catch (Clientprotocolexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}.start ();


} else {


Intent Intent = new Intent (Intent.action_view);
Uri data = Uri.parse (Savepath + "/" + filename);
if (Type.equals ("Document 22")) {
Intent.setdataandtype (data, "video/*");
} else {
Intent.setdataandtype (data, "application/*");
}
StartActivity (Intent);
}
}

public void Down_file (string url, string path) throws IOException {





URL myurl = new URL (URL);
URLConnection conn = Myurl.openconnection ();
Conn.connect ();
InputStream is = Conn.getinputstream ();
This.filesize = Conn.getcontentlength ();
if (this.filesize <= 0)
throw new RuntimeException ("Can not know the file ' s size");
if (is = = null)
throw new RuntimeException ("stream is null");
FileOutputStream fos = new FileOutputStream (path + "/" + filename);
FileOutputStream fos = new FileOutputStream (path + "/" + filename);
byte buf[] = new byte[1024];
downloadfilesize = 0;
sendmsg (0);
do {
Loop read
int numread = Is.read (BUF);
if (Numread = =-1) {
Break
}


Fos.write (buf, 0, Numread);
Downloadfilesize + = Numread;


Sendmsg (1);
} while (true);
Sendmsg (2);
try {
Is.close ();
} catch (Exception ex) {
LOG.E ("tag", "Error:" + ex.getmessage (), ex);
}
}

This is the control of ProgressBar, the use of thread thinking to do

Private Handler Handler = new Handler () {
@Override
public void Handlemessage (Message msg) {
if (! Thread.CurrentThread (). isinterrupted ()) {
Switch (msg.what) {
Case 0:
Mpbimage.setmax (fileSize);
Mpbtv.setmax (fileSize);
Case 1:
Mpbimage.setprogress (downloadfilesize);
Mpbtv.setprogress (downloadfilesize);
Break
Case 2:
Toast.maketext (Getactivity (), "Download is completed", 1)
. Show ();
Break


Case-1:
String error = Msg.getdata (). getString ("error");
Toast.maketext (getactivity (), error, 1). Show ();
Break
}
}
Super.handlemessage (msg);
}
};

Precautions:

1. Because the URL is transmitted over the service side, there will be no. DOC,.XSLX This result, so also to convert, according to Minetype conversion

for (int i = 0; i < mime_maptable.length; i++) {
if (Minetype! = null
&& minetype.equals (mime_maptable[i][1])) {
Producttpye = mime_maptable[i][0];
}
}


Private string[][] Mime_maptable = {


{". 3gp", "VIDEO/3GPP"},
{". apk", "application/vnd.android.package-archive"},
{". asf", "video/x-ms-asf"},
{". avi", "Video/x-msvideo"},
{". Bin", "Application/octet-stream"},
{". bmp", "Image/bmp"},
{". C", "Text/plain"},
{". Class", "Application/octet-stream"},
{". conf", "Text/plain"},
{". cpp", "Text/plain"},
{". doc", "Application/msword"},
{". docx",
"Application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
{". xls", "application/vnd.ms-excel"},
{". xlsx",
"Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
{". exe", "Application/octet-stream"},
{". gif", "Image/gif"},
{". Gtar", "Application/x-gtar"},
{". Gz", "Application/x-gzip"},
{". h", "Text/plain"},
{". htm", "text/html"},
{". html", "text/html"},
{". Jar", "application/java-archive"},
{". Java", "Text/plain"},
{". jpeg", "Image/jpeg"},
{". jpg", "Image/jpeg"},
{". js", "Application/x-javascript"},
{". Log", "Text/plain"},
{". m3u", "Audio/x-mpegurl"},
{". m4a", "AUDIO/MP4A-LATM"},
{". m4b", "AUDIO/MP4A-LATM"},
{". m4p", "AUDIO/MP4A-LATM"},
{". M4u", "Video/vnd.mpegurl"},
{". m4v", "video/x-m4v"},
{". mov", "Video/quicktime"},
{". Mp2", "Audio/x-mpeg"},
{". mp3", "Audio/x-mpeg"},
{". mp4", "Video/mp4"},
{". MPC", "Application/vnd.mpohun.certificate"},
{". Mpe", "Video/mpeg"},
{". mpeg", "Video/mpeg"},
{". mpg", "Video/mpeg"},
{". Mpg4", "Video/mp4"},
{". MPGA", "Audio/mpeg"},
{". Msg", "Application/vnd.ms-outlook"},
{". ogg", "Audio/ogg"},
{". pdf", "Application/pdf"},
{". png", "Image/png"},
{". pps", "Application/vnd.ms-powerpoint"},
{". ppt", "Application/vnd.ms-powerpoint"},
{". pptx",
"Application/vnd.openxmlformats-officedocument.presentationml.presentation"},
{". Prop", "Text/plain"}, {". rc", "Text/plain"},
{". rmvb", "Audio/x-pn-realaudio"}, {". rtf", "Application/rtf"},
{". Sh", "Text/plain"}, {". Tar", "Application/x-tar"},
{". tgz", "application/x-compressed"}, {". txt", "Text/plain"},
{". wav", "Audio/x-wav"}, {". wma", "audio/x-ms-wma"},
{". wmv", "audio/x-ms-wmv"},
{". wps", "Application/vnd.ms-works"}, {". xml", "Text/plain"},
{". Z", "Application/x-compress"},
{". zip", "application/x-zip-compressed"}, {"", "*/*"}};

The resulting producttpye is the suffix of the file. such as. jpg

If there is such a result is the best


Write this article is only to let oneself in the development of more smooth, poor voice expression, please forgive me, if you can not understand please leave a message, I will reply one by one:)

Android download files, files including videos and various files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.