Android 下載并打開PDF,Doc,Dwg文檔實例
今天項目中遇到這樣一個需求 ,根據(jù)后臺接口里pdf,doc,dwg文檔的地址 是一個URL ,需要根據(jù)文檔的url 下載到本地(內(nèi)部存儲或內(nèi)存卡)并用手機中能打開該文檔的軟件彈出來并打開,(這里需要做一個緩存,第一次查看這個文檔是在服務(wù)器上下載并打開,以后打開不需要下載直接打開本地的文檔)在網(wǎng)上找了些資料 寫了以下代碼,下面分享給大家;
效果圖:
代碼:
這是一個單獨的類 首先接收intent傳過來的url我是用url的后14位作為存儲本地的文件名(這里根據(jù)自己服務(wù)器的文件命名規(guī)則而定) 拿到文件路徑之后 判斷本地是否有此文件 有則打開沒有則從服務(wù)器上下載并打開 ;
Intent intent = act.getIntent(); final String Strname = intent.getStringExtra("docurl"); //截取最后14位 作為文件名 String s = Strname.substring(Strname.length()-14); //文件存儲 file1 = new File(Environment.getExternalStorageDirectory(), getFileName(s)); new Thread() { public void run() { File file = new File( file1.getAbsolutePath()); //判斷是否有此文件 if (file.exists()) { //有緩存文件,拿到路徑 直接打開 Message msg = Message.obtain(); msg.obj = haha; msg.what = DOWNLOAD_SUCCESS; handler.sendMessage(msg); mProgressDialog.dismiss(); return; } // 本地沒有此文件 則從網(wǎng)上下載打開 File downloadfile = downLoad(Strname, file1.getAbsolutePath(), mProgressDialog); // Log.i("Log",file1.getAbsolutePath()); Message msg = Message.obtain(); if (downloadfile != null) { // 下載成功,安裝.... msg.obj = downloadfile; msg.what = DOWNLOAD_SUCCESS; } else { // 提示用戶下載失敗. msg.what = DOWNLOAD_ERROR; } handler.sendMessage(msg); mProgressDialog.dismiss(); }; }.start();
下載文檔代碼;
傳入需要下載的文檔的url 和存入內(nèi)存的路徑和dialog
public static File downLoad(String serverpath, String savedfilepath, ProgressDialog pd) { try { URL url = new URL(serverpath); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); if (conn.getResponseCode() == 200) { int max = conn.getContentLength(); pd.setMax(max); InputStream is = conn.getInputStream(); File file = new File(savedfilepath); FileOutputStream fos = new FileOutputStream(file); int len = 0; byte[] buffer = new byte[1024]; int total = 0; while ((len = is.read(buffer)) != -1) { fos.write(buffer, 0, len); total += len; pd.setProgress(total); } fos.flush(); fos.close(); is.close(); return file; } else { return null; } } catch (Exception e) { e.printStackTrace(); } }
打開文件選擇器
Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case DOWNLOAD_SUCCESS: File file = (File) msg.obj; Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType (Uri.fromFile(file), "application/pdf"); // startActivity(intent); startActivity(Intent.createChooser(intent, "標題")); /** * 彈出選擇框之后 把本activity銷毀 */ finish(); break; case DOWNLOAD_ERROR: Util.showToast(act,"文件加載失敗"); break; } } };
整體代碼
public class list_item_doc extends BaseActivity { private ProgressDialog mProgressDialog; // 下載失敗 public static final int DOWNLOAD_ERROR = 2; // 下載成功 public static final int DOWNLOAD_SUCCESS = 1; private File file1; @Override protected void onCreate(Bundle arg0) { // TODO Auto-generated method stub super.onCreate(arg0); initView(); } private void initView() { // TODO Auto-generated method stub Intent intent = act.getIntent(); final String Strname = intent.getStringExtra("url"); mProgressDialog = new ProgressDialog(act); mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mProgressDialog.setCancelable(false); mProgressDialog.show(); //截取最后14位 作為文件名 String s = Strname.substring(Strname.length()-14); //文件存儲 file1 = new File(Environment.getExternalStorageDirectory(), getFileName(s)); new Thread() { public void run() { File haha = new File( file1.getAbsolutePath()); //判斷是否有此文件 if (haha.exists()) { //有緩存文件,拿到路徑 直接打開 Message msg = Message.obtain(); msg.obj = haha; msg.what = DOWNLOAD_SUCCESS; handler.sendMessage(msg); mProgressDialog.dismiss(); return; } // 本地沒有此文件 則從網(wǎng)上下載打開 File downloadfile = downLoad(Strname, file1.getAbsolutePath(), mProgressDialog); // Log.i("Log",file1.getAbsolutePath()); Message msg = Message.obtain(); if (downloadfile != null) { // 下載成功,安裝.... msg.obj = downloadfile; msg.what = DOWNLOAD_SUCCESS; } else { // 提示用戶下載失敗. msg.what = DOWNLOAD_ERROR; } handler.sendMessage(msg); mProgressDialog.dismiss(); }; }.start(); } /** * 下載完成后 直接打開文件 */ Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case DOWNLOAD_SUCCESS: File file = (File) msg.obj; Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType (Uri.fromFile(file), "application/pdf"); // startActivity(intent); startActivity(Intent.createChooser(intent, "標題")); /** * 彈出選擇框 把本activity銷毀 */ finish(); break; case DOWNLOAD_ERROR: Util.showToast(act,"文件加載失敗"); break; } } }; /** * */ /** * 傳入文件 url 文件路徑 和 彈出的dialog 進行 下載文檔 */ public static File downLoad(String serverpath, String savedfilepath, ProgressDialog pd) { try { URL url = new URL(serverpath); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); if (conn.getResponseCode() == 200) { int max = conn.getContentLength(); pd.setMax(max); InputStream is = conn.getInputStream(); File file = new File(savedfilepath); FileOutputStream fos = new FileOutputStream(file); int len = 0; byte[] buffer = new byte[1024]; int total = 0; while ((len = is.read(buffer)) != -1) { fos.write(buffer, 0, len); total += len; pd.setProgress(total); } fos.flush(); fos.close(); is.close(); return file; } else { return null; } } catch (Exception e) { e.printStackTrace(); return null; } } public static String getFileName(String serverurl) { return serverurl.substring(serverurl.lastIndexOf("/") + 1); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程簡單設(shè)置ListView分割線的方法
這篇文章主要介紹了Android編程簡單設(shè)置ListView分割線的方法,涉及Android布局簡單操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2017-02-02Android隱私協(xié)議提示彈窗的實現(xiàn)流程詳解
這篇文章主要介紹了Android隱私協(xié)議提示彈窗的實現(xiàn)流程,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01基于TabLayout中的Tab間隔設(shè)置方法(實例講解)
下面小編就為大家分享一篇基于TabLayout中的Tab間隔設(shè)置方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12Material Design系列之Behavior實現(xiàn)Android知乎首頁
這篇文章主要為大家詳細介紹了Material Design系列之Behavior實現(xiàn)Android知乎首頁的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09Android SQLite數(shù)據(jù)庫操作代碼類分享
這篇文章主要介紹了Android SQLite數(shù)據(jù)庫操作代碼類分享,本文直接給出實現(xiàn)代碼和使用代碼,需要的朋友可以參考下2015-03-03Android實現(xiàn)QQ新用戶注冊界面遇到問題及解決方法
這篇文章主要介紹了Android實現(xiàn)QQ新用戶注冊界面遇到問題及解決方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09Android 線程thread的兩種實現(xiàn)方法(必看)
下面小編就為大家?guī)硪黄狝ndroid 線程thread的兩種實現(xiàn)方法(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02模仿美團點評的Android應(yīng)用中價格和購買欄懸浮固定的效果
這篇文章主要介紹了模仿美團點評的Android應(yīng)用中價格和購買欄懸浮固定的效果,文章后半還針對快速滑動操作給出了一個響應(yīng)優(yōu)化的方法,需要的朋友可以參考下2016-04-04Android中通過樣式來去除app的頭及界面全屏(備忘)的實現(xiàn)方法
這篇文章主要介紹了Android中通過樣式來去除app的頭及界面全屏(備忘)的相關(guān)資料,需要的朋友可以參考下2016-12-12