android下載檔案,檔案包括視頻以及各種檔案

來源:互聯網
上載者:User

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.