Android(安卓)实现阅读pdf格式的文件和android 7.0以上版本出现FileUriExposedException的问题的解决
16lz
2021-01-24
1.实现pdf的阅读是一位大神给我说的,
因为Android不能像ios一样直接内置打开pdf,所以只能通过手机上自带的可以打开pdf的应用,这样也就是通过第三方的应用实现的,主要代码附上:
1》下载当前的文件的公共类
public class Downloader implements Callable{ protected int connectTimeout = 30 * 1000; protected int readTimeout = 1 * 1000 * 1000; protected int speedRefreshInterval = 500; protected byte[] buffer; private URL url; private File file; private float averageSpeed; private float currentSpeed; public Downloader() { buffer = new byte[8 * 1024]; } public void setUrlAndFile(URL url, File file) { this.url = url; this.file = file; this.averageSpeed = 0; this.currentSpeed = 0; } public URL getUrl() { return url; } public File getFile() { return file; } public float getAverageSpeed() { return averageSpeed; } public float getCurrentSpeed() { return currentSpeed; } @Override public File call() throws Exception { StopWatch watch = new StopWatch(); watch.start(); InputStream in = null; OutputStream out = null; try { URLConnection conn = url.openConnection(); conn.setConnectTimeout(connectTimeout); conn.setReadTimeout(readTimeout); conn.connect(); in = conn.getInputStream(); out = new FileOutputStream(file); int time = 0; int bytesInTime = 0; for (;;) { watch.split(); int bytes = in.read(buffer); if (bytes == -1) { break; } out.write(buffer, 0, bytes); time += watch.getTimeFromSplit(); if (time >= speedRefreshInterval) { currentSpeed = getSpeed(bytesInTime, time); time = 0; bytesInTime = 0; } } } catch (IOException e) { file.delete(); throw e; } finally { if (in != null) { try { in.close(); } catch (IOException e) { } } if (out != null) { try { out.flush(); out.close(); } catch (IOException e) { } } } watch.stop(); averageSpeed = getSpeed(file.length(), watch.getTime()); return file; } private static float getSpeed(long bytesInTime, long time) { return (float) bytesInTime / 1024 / ((float) time / 1000); }}
2》调用
private void getPDF() { PerssionUtils.verifyStoragePermissions(this); URL url = null; try { url = new URL(docUrl);//docUrl为pdf地址 String path = Environment.getExternalStorageDirectory() + "/Download/"; String[] splitStrings = docUrl.split("/"); String[] nameStrings = splitStrings[splitStrings.length - 1] .split(".pdf"); String fileName = nameStrings[0] + ".pdf";//当pdf的url带签名时,文件名过长下载会失败 File file = new File(path + fileName); ExecutorService executorService = Executors .newSingleThreadExecutor(); Downloader downloader = new Downloader(); downloader.setUrlAndFile(url, file); File downloadFile = executorService.submit(downloader) .get(); // loadingDialog.dismiss(); Uri path1 = Uri.fromFile(downloadFile); Intent intent = new Intent(Intent.ACTION_VIEW); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.setDataAndType(path1, "application/pdf"); try { this.startActivity(Intent.createChooser(intent,"")); } catch (ActivityNotFoundException e) { ToastUtil.show(this, "打开失败"); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); }}
2.关于7.0以上的调用 有2种方法
第一种方法百度有很多,比较麻烦,大家都能查到
第二种方法: Intent.createChooser()这个方法是启动应用选择器
Intent intent = new Intent(Intent.ACTION_VIEW);intent.addCategory(Intent.CATEGORY_DEFAULT);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
更多相关文章
- 解决URl中文路径乱码问题
- Android(安卓)Studio编译输出apk文件修改文件名
- Android对不同DPI的dimen选择优先级问题
- Android(安卓)Studio中导入依赖库的方法
- Android反编译工具的使用-Android(安卓)Killer
- Android(安卓)Fragmnet的使用新体会
- android fragment 切换时隐藏软键盘
- Android中的文件存储数据方式 .
- Android(安卓)TextView文字滚动