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

Java 试图使用fileProvider从资产文件夹打开PDF文件,但它给出FileNotFoundException:没有这样的文件或目录,java,android,pdf,uri,android-fileprovider,Java,Android,Pdf,Uri,Android Fileprovider,我正在尝试显示存储在android应用程序资产文件夹中的“NEFT.pdf”文件。 下面的代码在API 25之前工作得非常好 private void CopyReadAssets(String filename) { AssetManager assetManager = getAssets(); InputStream in = null; OutputStream out = null; File file = new File(getFilesDir(),

我正在尝试显示存储在android应用程序资产文件夹中的“NEFT.pdf”文件。
下面的代码在API 25之前工作得非常好

private void CopyReadAssets(String filename) {
    AssetManager assetManager = getAssets();
    InputStream in = null;
    OutputStream out = null;
    File file = new File(getFilesDir(), filename);

    try {
        in = assetManager.open(filename);
        out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

        copyFile(in, out);
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;


        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(
                Uri.parse("file://" + getFilesDir() + "/"+filename), "application/pdf");

        startActivity(intent);
    } catch (Exception e)
    {
        Toast.makeText(PdfFilesList.this, "cra: "+e.toString(), Toast.LENGTH_SHORT).show();
    }
}
此代码不适用于API 25及以上版本。它提供了不再支持的错误模式\u WORLD\u READABLE
我将其更改为模式\u PRIVATE,但这给了我另一个错误

android.os.fileuriexposedexception通过intent.getdata()在应用程序之外公开。
因此,我应用了在中解释的概念
这是我在错误日志中得到的信息:

E/DisplayData:openFd:java.io.FileNotFoundException:没有这样的文件或目录

E/PdfLoader:无法加载文件(未打开)显示数据[PDF:NEFT.PDF]+内容可打开,uri:content://com.user.plansmart.provider/pdf_files/NEFT.pdf

在我看来,乌里是对的。有人能帮我找到这里的错误吗。

这是清单文件中的提供程序元素

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

由于您希望在单独的应用程序(如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" />

由于您希望在单独的应用程序(如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" />


如果资产中有文件,则不能使用FileProvider类为其提供服务。重新思考。重新编码。
getFilesDir(),“pdf”)。你说什么?资产?不,不是资产。getFilesDir()是私有内部存储。请调整你的帖子。从主题开始。或者文件是否在资产中?那么就不能使用getFilesDir()。请告诉我你有什么。是的,我的pdf文件在资产文件夹中。如果我做错了,请告诉我正确的方法。你至少有两个选择。将文件从资产复制到GetFileDir()。然后你可以使用你发布的代码。如果你选择这条路,将会有一个错误,但以后会有更多的错误。此外,您还可以编写自己的文件提供程序,该程序可以为资产中的文件提供服务。如果资产中有文件,则无法使用FileProvider类为其提供服务。重新思考。重新编码。
getFilesDir(),“pdf”)。你说什么?资产?不,不是资产。getFilesDir()是私有内部存储。请调整你的帖子。从主题开始。或者文件是否在资产中?那么就不能使用getFilesDir()。请告诉我你有什么。是的,我的pdf文件在资产文件夹中。如果我做错了,请告诉我正确的方法。你至少有两个选择。将文件从资产复制到GetFileDir()。然后你可以使用你发布的代码。如果你选择这条路,将会有一个错误,但以后会有更多的错误。此外,您还可以编写自己的文件providet,它可以为资产中的文件提供服务。“文件不存在”。我试图将Uri路径更改为以下Uri路径=Uri.parse(“content://com.user.plansmart.provider/pdf_files/“+文件名);还尝试了Uri path=Uri.parse(“file:///android_asset/“+文件名);不要把时间花在这个答案上。胡说八道,成功了。out=openFileOutput(file.getName(),Context.MODE\u WORLD\u READABLE);方法是罪魁祸首,当更改为out=newbufferedoutputstream(newfileoutputstream(file));它给出了期望的结果。非常感谢,“文件不存在”。我试图将Uri路径更改为以下Uri路径=Uri.parse(“content://com.user.plansmart.provider/pdf_files/“+文件名);还尝试了Uri path=Uri.parse(“file:///android_asset/“+文件名);不要把时间花在这个答案上。胡说八道,成功了。out=openFileOutput(file.getName(),Context.MODE\u WORLD\u READABLE);方法是罪魁祸首,当更改为out=newbufferedoutputstream(newfileoutputstream(file));它给出了期望的结果。非常感谢。