android下载文件,文件包括视频以及各种文件

在做项目中要进行文件的下载以及调用系统软件打开,文件是服务端,以下是一些代码片段:

运用progressbar和http协议下载文件:

如果没有这个文件的就先进行创建并下载,如果有的话就打开:

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 {
// 循环读取
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);
}
}

这个是控制progressbar的,利用线程的思维去做

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);
}
};

注意事项:

1.由于url是服务端传过来的,所以不会有.doc,.xslx这种的结果,所以还要进行转换,根据mineType的转化

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" }, { "", "*/*" } };

得到的productTpye 就是文件的后缀名。如.jpg

如果有这种结果的是最好


写下此文仅是让自己在开发中更加顺利,语音表达欠佳,请各位多多包涵,如果看不懂请留言,我会逐个回复的 :)

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。