下载并打开pdf文件,前提是手机上有可打开pdf文件的应用
System.out.println("我点击了按钮");
String urlString = "http://public.dhe.ibm.com/common/ssi/ecm/en/wsd14109usen/WSD14109USEN.PDF";
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection)
url.openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
//实现连接
connection.connect();
System.out.println("connection.getResponseCode()="+connection.getResponseCode());
if (connection.getResponseCode() == 200) {
InputStream is = connection.getInputStream();
//以下为下载操作
byte[] arr = new byte[1];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(baos);
int n = is.read(arr);
while (n > 0) {
bos.write(arr);
n = is.read(arr);
}
bos.close();
String path = Environment.getExternalStorageDirectory()
+ "/download/";
String[] name = urlString.split("/");
path = path + name[name.length - 1];
System.out.println("name="+name);
System.out.println("path="+path);
File file = new File(path);
FileOutputStream fos = new FileOutputStream(file);
fos.write(baos.toByteArray());
fos.close();
//关闭网络连接
connection.disconnect();
System.out.println("下载完成");
if (file.exists()) {
System.out.println("打开");
Uri path1 = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path1, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
System.out.println("打开失败");
}
}
}
} catch (IOException e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
分享到:
相关推荐
Android 插件式打开PDF文件
Android扫描本地pdf文件和从网络下载pdf文件并打开实现文件的网络下载和打开,PDF依赖便捷好用
Android 中打开pdf文件也是一种很常见的场景,本文主要介绍了Android打开本地pdf文件的方法。具有很好的参考价值。下面跟着小编一起来看下吧
在Android应用中打开PDF文件,实现先查找本地pdf文件,如果有直接打开没有去下载PDF文件用于打开。打开PDF时跳转手机上能打开PDF文件的软件直接查看,如果点开查看时有问题则会在Android页面中直接打开PDF文件进行...
实现了在APP内部打开pdf文件,加载速度快,能够解决工作中有需要操作office的需求
android下载并打开网络上的pdf文件,6.0以上需要申请权限,到时候自己写一下就行了
用于android中打开pdf文件。基于mupdf-1.6 版本进行编译打包成动态库(libmupdf.so),可以自己封装成 .jar,提供到各自的app中使用
AndroidPDF用于在App中打开PDF文件,支持网络
通过地址将pdf下载到手机内存中,然后打开。bug已经修复 已测试,好用。
pdfview打开pdf文件,避免android无法通过webview打开pdf文件 网上有studio版本的,这里我改成eclipse版本,有需要的朋友可以下载
android打开pdf文件的apk工程
Android 插件式打开PDF文件.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
我的方案是:首先将PDF文件下载到手机本地,然后通过调用手机端的应用打开。该Demo是从Android项目中分离出来,完整的保存了一个项目中所使用到的相关机制,线程下载,文件保存,人性化的下载图标,自定义的Toast等...
这个也可以直接代开PDF,原理是每次下载后再打开,而且内库比较大,APK会增加20多兆这个是缺点,记住直接复制过去,不要更改里面的文件夹名字,否则报错
android用于打开各种文件的intent.pdf
android 系统调用WPS打开word excel ppt pdf 等,将代码类移入的你项目,有对6.0 7.0及以上系统做文件读取方法
Android WebView 加载 PDF 文件实现。其原理就是在 app 本地搭建一个 website ,然后利用 WebView 加载这个站点,站点去实现加载显示 PDF 的目的,站点其原理也是采用 js 框架去实现的。
android打开pdf的工具,兼容性比较好,比较实用。安卓加载pdf的方式比较多,还有几种后续补充
Android中内嵌MuPDF实现打开PDF文件
这是一个简单的pdf阅读器,不用第三方软件打开pdf文件,个别文件预览会出错,可作为新手参考