Android 下载并打开PDF,Doc,Dwg文档实例

今天项目中遇到这样一个需求 ,根据后台接口里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); } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

时间: 2024-08-30 04:34:59

Android 下载并打开PDF,Doc,Dwg文档实例的相关文章

怎样在网页中打开WORD格式的文档?超连接打开的时候总是提示下载文件怎么办?

问题描述 我有个WORD格式的质量手册,现在要挂到WEB服务器上,网上有资料说可以用超连接直接显示:<iframesrc="./11.doc"></iframe>我试了一下,打开页面的时候浏览器总是提示下载文件:打开还是保存.怎样让浏览器直接打开而不出现下载提示?或者还有其他办法在网页中打开WORD格式的文档?谢谢! 解决方案 解决方案二:web.xml下加<mime-mapping><extension>doc</extensio

《善用佳软:高效能人士的软件应用之道》一2.5 PDF:跨平台文档解决方案

2.5 PDF:跨平台文档解决方案 善用佳软:高效能人士的软件应用之道 2.5.1 全面接触PDF:最好用的PDF软件汇总1 本节旨在介绍最实用的.以免费软件为主的PDF相关工具,同时消除对PDF的常见误解,较为系统地介绍PDF应用操作,包括PDF虚拟打印机.格式转换.阅读及注释.属性及页面编辑.图文编辑等. 1.关于PDF及本节内容的一些基本说明 (1)关于PDF. 什么是PDF?PDF是由Adobe公司发明的文件格式,是Portable Document Format的缩写,意为"便携文档格

Java实现web在线预览office文档与pdf文档实例

1.首先我们需要找到可以把office转换成pdf的方法,查找资料发现有openoffice这一软件可以把office转换成pdf,这一软件先下载下来,然后记住自己安装的在那个位置.然后在cmd环境下进入安装目录的program目录,输入打开openoffice的命令:soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard 输入完成之后在任务管理器可以看见soffice.b

如何打开SkyDrive上的文档

SkyDrive是微软提供在云的一个存储空间,您下载SkyDrive客户端,然后创建一个微软的帐号,与本地的计算机同步,就可以实现资源的共享和存储,在本文,我将教你如何通过本地的Office软件直接打开SkyDrive上的文档. 这里我们使用的是微软最新的办公软件 Office2013,您可以直接从微软的官网上下载试用版 接下来我们以Word 2013来简单做个例子: 一.下载并安装完软件之后,直接打开Word2013,主界面如下: 二.点击"Open"选项,如下,您就会看到有一个&q

在Word2010中打开最近使用的文档

在Word2010中默认会显示20个最近打开或编辑过的Word文档,用户可以通过"最近"面板打开最近使用的文档,操作步骤如下所述: 第1步,打开Word2010文档窗口,单击"文件"按钮,如图4所示. 图4 单击"文件"按钮第2步,在"文件"面板右侧的"最近使用的文档"列表中单击准备打开的Word文档名称即可,如图5所示. 图5 单击最近使用的Word文档 三联推荐:点击免费下载最新版WPS办公软件   

solr从pdf、office文档中建立索引

2015年05月28日 ⁄ hadoop ⁄ 评论数 1 使用solr从pdf.office文档中建立索引和从数据库中建立相似,只不过这里需要tika来解析这些文档.8.1 配置一个handler 这个handler首先要在solrConfig.xml中配置,如下所示:                   <requestHandler name="/dataimport"    class="org.apache.solr.handler.dataimport.Dat

word文档-Android 如何将一个文本保存成Word文档

问题描述 Android 如何将一个文本保存成Word文档 问题如题,在Android中如何将 一个内容 保存成word文档 解决方案 可以用POI来做.. 解决方案二: 有格式么 ? 没有直接建一个文件.doc

如何在Word 2010中打开最近使用的文档

在Word 2010中默认会显示20个最近打开或编辑过的Word文档,用户可以通过"最近"面板打开最近使用的文档,操作步骤如下所述: 第1步,打开Word 2010文档窗口,单击"文件"按钮,如图2009120504所示. 图2009120504 单击"文件"按钮 第2步,在"文件"面板右侧的"最近使用的文档"列表中单击准备打开的Word文档名称即可,如图2009120505所示. 图2009120505

XP系统无法打开我的电脑和我的文档怎么办?

  XP系统无法打开我的电脑和我的文档怎么办? 1.新建记事本复制下列命令粘贴到记事本中. regsvr32 appwiz.cpl regsvr32 mshtml.dll regsvr32 jscript.dll regsvr32 msi.dll regsvr32 "C:Program Files Common Files System Ole DB Oledb32.dll" regsvr32 "C:Program Files Common Files System Ado