Error trying to find file in Android app with File Provider

Asked

Viewed 244 times

0

Hello, I’m having a hard time with Fileprovider.

The file comes to be created because I checked manually in the directories. I put some logs to see what happens but still can not understand why he does not find

I send a PDF but I can’t open it right away. Below is the code.

Androidmanifest.xml

 <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/filepaths" />
        </provider>

filepaths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-files-path name="files" path="."/>
</paths>

creation of pdf file

File docsFolder = new File(Environment.getExternalStorageDirectory()+"/compras");
        if (!docsFolder.exists()) {
            docsFolder.mkdir();
            Log.i(TAG, "Created a new directory for PDF");
        }

        comprovante = new File(docsFolder.getAbsolutePath(),"comprovante.pdf");

Attempt to upload the file

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = FileProvider.getUriForFile(ComprasComprovanteActivity.this, BuildConfig.APPLICATION_ID + ".provider",comprovante);

The mistake

Failed to find configured root that contains /storage/emulated/0/compras/comprovante.pdf
  • See if the way is this msm, does not seem error of permission, it seems that the way is wrong msm

1 answer

0


To solve the problem we had to add the two attributes below to the Intent.

intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.