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

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

尝试使用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" />


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5021004.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存