java.lang.SecurityException:权限拒绝:打开提供程序 androidx.core.content.FileProvider

问题描述

所以我一直在制作这两个应用程序,我希望应用程序 1 能够将 pdf 文件发送到应用程序 2。它们都使用不同的密钥进行签名,因为应用程序 1 也应该可以被任何其他应用程序使用。

我希望它的工作方式如下。应用程序 2 将 startActivityForResult 发送到应用程序 1,其意图包括带有来自应用程序 2 文件提供程序的文件的 Uri。 然后应用程序 1 应获取 Uri 并将 pdf 写入应用程序 2 提供的文件。然后返回应用程序 2,该文件应首先写入应用程序 2 拥有的文件中。

简单地说,应用程序 2 应提供一个文件,应用程序 1 应在其中编写 pdf。

所以我把它全部设置成这样,但是在尝试访问文件以写入文件时,我收到了 java.lang.SecurityException: Permission Denial: opening provider androidx.core.content.FileProvider 错误消息。

完整的错误信息:Permission Denial: opening provider androidx.core.content.FileProvider from ProcessRecord{511cc8 14209:com.eureka.documentscanner/u0a159} (pid=14209,uid=10159) that is not exported from UID 10155

复制pdf的代码

//This method is called once the app has selected the right pdf file and wants to return back to app 2.
    private void writetoSharedFile(){
        Intent StartingIntent = getIntent();

        //Get the shared file (the file to write to from app 2).
        Uri sharedFileUri = StartingIntent.getParcelableExtra(Intent.EXTRA_STREAM);

        //Get the file that I need to write into the shared file.
        Uri filetoWrite = Uri.fromFile(scanResult.pdfFile);

        //Write the file into the shared file.
        copyPDFs(filetoWrite,sharedFileUri);
    }


    private void copyPDFs(Uri from,Uri to){
        ParcelFileDescriptor pfdTo = null;
        FileOutputStream out = null;
        ParcelFileDescriptor pfdFrom = null;
        FileInputStream in = null;
        ContentResolver cr = context.getContentResolver();
        final String WRITE_ACCESS = "w";
        final String READ_ACCESS = "r";

        try {
            pfdTo = cr.openFileDescriptor(to,WRITE_ACCESS);
            out = new FileOutputStream(pfdTo.getFileDescriptor());

            pfdFrom = cr.openFileDescriptor(from,READ_ACCESS);
            in = new FileInputStream(pfdFrom.getFileDescriptor());

            //Reads from the input stream and writes the data to the output stream.
            int n;
            while ((n = in.read()) != -1) {
                out.write(n);
            }
        } catch (SecurityException e){
            e.printstacktrace();
        } catch (IOException e) {
            e.printstacktrace();
        }
        finally {
            // Let the document provider kNow you're done by closing the stream.
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printstacktrace();
                }
            }

            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printstacktrace();
                }
            }

            if (pfdTo != null) {
                try {
                    pfdTo.close();
                } catch (IOException e) {
                    e.printstacktrace();
                }
            }

            if (pfdFrom != null) {
                try {
                    pfdFrom.close();
                } catch (IOException e) {
                    e.printstacktrace();
                }
            }
        } //Close the Streams and Descriptors.
    }

安全异常总是在 pfdTo = cr.openFileDescriptor(to,WRITE_ACCESS); 行抛出。

我 100% 确定对用于启动结果的活动的意图具有读写访问权限,并且对外部存储授予了读写权限。

任何建议或帮助将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)