Android 10 - file provider - permission denial: reading android.support.v4.content.FileProvider uri

Charles Chung picture Charles Chung · Feb 21, 2020 · Viewed 7.4k times · Source

I've tried to generate a CSV file (of Accelerometer), save it to my Context.getFilesDir(), and then use FileProvider to share it to Google Drive.

The problem is when I try to share it, error Writing exception to parcel, java.lang.SecurityException: Permission Denial: reading android.support.v4.content.FileProvider uri content://...csv requires the provider be exported, or grantUriPermission() keeps occurring.

I had...

  1. Added grantUriPermission() and Intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) as others suggests.
  2. I tried to write to Environment.getExternalStorageDirectory(), but it doesn't work since my phone doesn't have external storage.
  3. I can not export the provider because it raises an error as well.

What can I do then? Or is that a better way to generate and share a CSV file? Any suggestions would be grateful. Thank you, guys.

Code Snippet for now

In AndroidManifest.xml:

    <application...>
        ...

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

    </application>

In provider_path.xml

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

In mainActivity.java

    private void onShareCSV() {

        Uri path = FileProvider.getUriForFile(this, "com.example.FileProvider", new File(filePath + fileName));

        Log.d(TAG, "onShareCSV: " + path);
        Intent shareIntent = new Intent("com.example.FileProvider").setData(path);

        List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(shareIntent, PackageManager.MATCH_DEFAULT_ONLY);
        for (ResolveInfo resolveInfo : resInfoList) {
            String packageName = resolveInfo.activityInfo.packageName;
            getApplicationContext().grantUriPermission(packageName, path, Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }

        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_TEXT, "This is a CSV I'm sharing.");
        shareIntent.putExtra(Intent.EXTRA_STREAM, path);
        shareIntent.setType("text/csv");
        startActivity(Intent.createChooser(shareIntent, "Share..."));
        this.setResult(RESULT_OK, shareIntent);
    }

And here is the error

2020-02-20 23:45:02.751 21484-21503/com.example.charleschung.mci_pa1 E/DatabaseUtils: Writing exception to parcel
    java.lang.SecurityException: Permission Denial: reading android.support.v4.content.FileProvider uri content://com.example.FileProvider/files/sensor-1582260302226.csv from pid=21581, uid=1000 requires the provider be exported, or grantUriPermission()
        at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:729)
        at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:602)
        at android.content.ContentProvider$Transport.query(ContentProvider.java:231)
        at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:104)
        at android.os.Binder.execTransactInternal(Binder.java:1021)
        at android.os.Binder.execTransact(Binder.java:994)

Answer

alireza rajaei picture alireza rajaei · Feb 22, 2020

first of all you should to check build version code

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        Uri path = FileProvider.getUriForFile(this,getApplicationContext().getPackageName() +  ".FileProvider", new File(filePath + fileName)); 

                    } else{
                        Uri path =Uri.fromFile(new File(filePath + fileName)) 
                    }

i hope its helpfull