Android下载并打开PDF文件
2017-07-06 16:27
549 查看
1.下载PDF文件到本地
private void downFile(){ String urlString = "http://14.215.72.79/file3.data.weipan.cn/61710973/8e6cfb727a439608032a222755e9c8e366cfb252?ip=1499330937,183.14.31.194&ssig=gpMUwXAXDI&Expires=1499331537&KID=sae,l30zoo1wmz&fn=%E6%9D%83%E5%A8%81%E6%95%B0%E7%8B%AC%E5%85%A5%E9%97%A8%E5%88%B0%E7%B2%BE%E9%80%9A.pdf&skiprd=2&se_ip_debug=183.14.31.194&corp=2&from=1221134&wsiphost=local"; 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(); 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]; File file = new File(path); FileOutputStream fos = new FileOutputStream(file); fos.write(baos.toByteArray()); fos.close(); //关闭网络连接 connection.disconnect(); Log.d("下载完成","下载完成"); openPDF(file);//打开PDF文件 } } catch (Exception e) { // TODO: handle exception System.out.println(e.getMessage()); } }
2.打开PDF文件
private void openPDF(File file) {if (file.exists()) {
Log.d("打开","打开");
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 (Exception e) {
Log.d("打开失败","打开失败");
}
}
}
3.新建一个线程调用下载方法
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View view) {
new Thread(){
@Override
public void run() {
super.run();
downFile();
}
}.start();
}
});
注意:有些手机可能默认了文件的打开方式为WPS,WPS软件有时候是不能打开PDF文件的,这时就会出现一闪而过的情况
相关文章推荐
- Android下载并打开PDF文件
- Android:下载并打开PDF格式文件2----用指定的software
- Android:下载后打开PDF格式文件
- Android:下载后打开PDF格式文件
- 在线打开.PDF、.TXT等文件,.doc、.xls自动下载
- android Intent打开各种类型文件(PDF、word、excel、ppt、chm)
- android下载后打开pdf
- Android中打开各种格式的文件(apk、word、excel、ppt、pdf、音视频、图片等)
- 轻松下载.pdf文件(直接下载,而不是在IE浏览器里面打开)
- android打开pdf ppt word excel chm html text 文件的intent
- Android 下载文件并调用相关插件打开
- Xamarin.Android - 下载pdf和视频到应用空间并打开
- android Intent打开各种类型文件((PDF、word、excel、ppt、chm)
- 轻松下载.pdf文件(直接下载,而不是在IE浏览器里面打开)
- android Intent打开各种类型文件(PDF,PPT,WORD,EXCEL,CHM,H...
- android用于打开各种文件的intent,包括以下文件PDF,PPT,WORD,EXCEL,CHM,HTML,TEXT,AUDIO,VIDEO
- android Intent打开各种类型文件((PDF、word、excel、ppt、chm)
- android用于打开各种文件的intent,包括以下文件PDF,PPT,WORD,EXCEL,CHM,HTML,TEXT,AUDIO,VIDEO
- Android打开pdf文件
- Android 打开pdf文档,没有阅读器链接到Google Play Store等下载