android下載檔案並開啟
阿新 • • 發佈:2019-01-05
首先是下載:
public static void downLoadFromStream(final String urlString, final String fileType, final String resourceId) { final ProgressDialog progressDialog = new ProgressDialog(webView.getContext()); progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); progressDialog.setCancelable(false); progressDialog.setCanceledOnTouchOutside(false); progressDialog.setMessage("檔案獲取中,請稍候....."); progressDialog.show(); String dirPath = ""; dirPath = Environment.getExternalStorageDirectory() + File.separator+"download"+File.separator //建立檔案下載的目錄 final File dir = new File(dirPath); if (!dir.exists()) { dir.mkdirs(); } final Runnable runnable = new Runnable() { @Override public void run() { int byteread = 0; try { final String fileName = resourceId + "." + fileType;//把resourceId 與filetype拼接成檔案的名稱 final File file = new File(dir, fileName); //在剛剛建立好的目錄下建立檔案 file.createNewFile(); final URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); InputStream inputStream = connection.getInputStream(); OutputStream outputStream = new FileOutputStream(file); byte buffer[] = new byte[4 * 1024]; while ((byteread = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, byteread); } outputStream.close(); inputStream.close(); connection.disconnect(); progressDialog.dismiss(); openFile(webView.getContext(), file); } catch (Exception e) { e.printStackTrace(); } } }; new Thread(runnable).start(); }
這裡 需要注意:可以先判斷
if(connection.getContentLength() >0){
}
再去接受檔案流。但是有些情況伺服器端不返回ContentLength,所以這個事先跟服務端約定好。如果是別人的服務的話有請求連結用瀏覽器開啟看看請求就知道了(之後會寫個部落格說說我的理解)
2、
接下來是開啟檔案
先來個腦殘版的,這個相當的坑,從網上考過來的。
private static void openFile(Context context, File f) { Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW); String mimetype = getMIMEType(f); myIntent.setDataAndType(Uri.fromFile(f),mimetype); context.startActivity(myIntent); }
/* 判斷檔案MimeType的method */ private static String getMIMEType(File f) { String type = ""; String fName = f.getName(); /* 取得副檔名 */ String end = fName.substring(fName.lastIndexOf(".") + 1, fName.length()).toLowerCase(); /* 依副檔名的型別決定MimeType */ if (end.equals("m4a") || end.equals("mp3") || end.equals("mid") || end.equals("xmf") || end.equals("ogg") || end.equals("wav")) { type = "audio"; } else if (end.equals("3gp") || end.equals("mp4")) { type = "video"; } else if (end.equals("jpg") || end.equals("gif") || end.equals("png") || end.equals("jpeg") || end.equals("bmp")) { type = "image"; } else if (end.equals("wps")) { type = "application/vnd.ms-works"; } else if (end.equals("doc")) { type = "application/msword"; } else if (end.equals("docx")) { type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; } else if (end.equals("txt")) { type = "text/plain"; } else if (end.equals("pdf")) { type = "application/pdf"; } else if (end.equals("apk")) { /* android.permission.INSTALL_PACKAGES */ type = "application/vnd.android.package-archive"; } else { type = "*"; } /*如果無法直接開啟,就跳出軟體列表給使用者選擇 */ if (end.equals("apk")) { } else { type += "/*"; } return type; }
以上這個方法自己判斷檔案型別,費時費力。而且有很大的問題:a、預設的開啟軟體相當多,能翻個好幾頁。 b、有的時候會報錯android.content.ActivityNotFoundException: No Activity found to handle Intent (我的猜想是type型別自己寫的很肯定會有所出入) 相當的蛋疼。
來個漂亮的:
private static void openFile(Context context, File f) {
Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW);
String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(f).toString());
String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
myIntent.setDataAndType(Uri.fromFile(f),mimetype);
context.startActivity(myIntent);
}
系統提供的解析檔案型別的方法,很強大。不會出現上述的情況了。