Android 下载并打开PDF,Doc,Dwg文档实例
程序员文章站
2022-08-02 14:22:03
今天项目中遇到这样一个需求 ,根据后台接口里pdf,doc,dwg文档的地址 是一个url ,需要根据文档的url 下载到本地(内部存储或内存卡)并用手机中能打开该文档的软...
今天项目中遇到这样一个需求 ,根据后台接口里pdf,doc,dwg文档的地址 是一个url ,需要根据文档的url 下载到本地(内部存储或内存卡)并用手机中能打开该文档的软件弹出来并打开,(这里需要做一个缓存,第一次查看这个文档是在服务器上下载并打开,以后打开不需要下载直接打开本地的文档)在网上找了些资料 写了以下代码,下面分享给大家;
效果图:
代码:
这是一个单独的类 首先接收intent传过来的url我是用url的后14位作为存储本地的文件名(这里根据自己服务器的文件命名规则而定) 拿到文件路径之后 判断本地是否有此文件 有则打开没有则从服务器上下载并打开 ;
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; } // 本地没有此文件 则从网上下载打开 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 和存入内存的路径和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; } // 本地没有此文件 则从网上下载打开 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); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。