8

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)
Charles Chung
  • 103
  • 1
  • 1
  • 10
  • `Uri path = FileProvider.getUriForFile(this........`. Please tell the value of `path.toString()`. – blackapps Feb 21 '20 at 06:59
  • @blackapps thanks for your reply, I've re-format the snippet. The path value is 'content://com.example.FileProvider/files/sensor-1582271002304.csv'. – Charles Chung Feb 21 '20 at 07:51
  • `List 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); }` Remove that code please. You dont need it. – blackapps Feb 21 '20 at 08:52
  • Thanks, but after removing that code nothing changed. I there any other thing I can do? – Charles Chung Feb 21 '20 at 19:12
  • stackoverflow.com/a/74052302/987762 for answer – kalandar Oct 13 '22 at 08:05

3 Answers3

1

I finally worked it out. Tunes out my URI construction has some problems. I changed it to

Context context = getApplicationContext();
File filelocation = new File(getFilesDir(), fileName);
Uri path = FileProvider.getUriForFile(context, "com.example.charleschung.mci_pa1", filelocation);

And it works. Hope it helps :)

Charles Chung
  • 103
  • 1
  • 1
  • 10
0

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

-4

I was also having this issue but it was little bit different from yours i was trying to save file in a folder. What i was missing was permission for android 10 So i added this line in manifest

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>

Also one more line in android:requestLegacyExternalStorage="true"

Add runtime Permission as well

Manifest.permission.ACCESS_MEDIA_LOCATION

Muhib
  • 9
  • 3