栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

尝试使用fileProvider从资产文件夹中打开PDF文件,但它给出FileNotFoundException:没有这样的文件或目录

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

尝试使用fileProvider从资产文件夹中打开PDF文件,但它给出FileNotFoundException:没有这样的文件或目录

因为您想在单独的应用程序(例如Adobe Reader)中显示PDF文件,所以我希望执行以下操作:

private void CopyReadAssets()        { AssetManager assetManager = getActivity().getAssets(); InputStream in = null; OutputStream out = null; String state = Environment.getExternalStorageState(); if (!Environment.MEDIA_MOUNTED.equals(state)) {     Toast.makeText(getActivity(), "External Storage is not Available", Toast.LENGTH_SHORT).show(); } File pdfDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDFs"); if (!pdfDir.exists()) {     pdfDir.mkdir(); } File file = new File(pdfDir + "/abc.pdf"); try {     in = assetManager.open("abc.pdf");     out = new BufferedOutputStream(new FileOutputStream(file));     copyFile(in, out);     in.close();     in = null;     out.flush();     out.close();     out = null; } catch (Exception e) {     Log.e("tag", e.getMessage()); } if (file.exists()) //Checking for the file is exist or not {     Uri path = Uri.fromFile(file);     Intent objIntent = new Intent(Intent.ACTION_VIEW);     objIntent.setDataAndType(path, "application/pdf");     objIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);     Intent intent1 = Intent.createChooser(objIntent, "Open PDF with..");     try {         startActivity(intent1);     } catch (ActivityNotFoundException e) {         Toast.makeText(getActivity(), "Activity Not Found Exception ", Toast.LENGTH_SHORT).show();     } } else {     Toast.makeText(getActivity(), "The file not exists! ", Toast.LENGTH_SHORT).show(); }        }

要将文件复制到设备内存中:

private void copyFile(InputStream in, OutputStream out) throws IOException    {        byte[] buffer = new byte[1024];        int read;        while ((read = in.read(buffer)) != -1)        { out.write(buffer, 0, read);        }    }

使用以下权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/410540.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号