Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android下載文件,文件包括視頻以及各種文件

android下載文件,文件包括視頻以及各種文件

編輯:關於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

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved