1

I have a library which needs to have a fileprovider and in order to prevent conflict with the implementing app's FileProvider, I used the approach mentioned here. Although the library has no issues building and the library tested in the sample application, It throws the below exception.

Note: Despite the exception, I'm able to share the content out to other apps using share intent. I'm going clueless why the exception occurs while still the files are shared without any crash.

Exception:

E/DatabaseUtils: Writing exception to parcel
java.lang.SecurityException: Permission Denial: reading com.library.package.reporter.FilesProvider uri content://com.library.package.sample.com.library.package.provider.fileprovider/./reports/2020-03-28%2022%3A42%3A50_report.txt from pid=3158, 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)

Manifest:

<provider
        android:name=".reporter.FilesProvider"
        android:authorities="${applicationId}.com.library.package.provider.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/filepaths" />
    </provider>

FileProvider Usage:

Uri uri = FilesProvider.getUriForFile(getApplicationContext(), getPackageName() + ".com.library.package.provider.fileprovider", new File(filePath));
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_TEXT, content);
intent.putExtra(Intent.EXTRA_STREAM, uri);
context.startActivity(Intent.createChooser(intent, context.getString(R.string.intent_share_title)));

Should I just ignore the crash and move on or is there something I'm doing wrong?

Vijai
  • 1,067
  • 2
  • 11
  • 25
  • 1
    "is there something I'm doing wrong?" -- probably. You might want to edit your question and provide the code that is using `FileProvider.getUriForFile()`. – CommonsWare Mar 28 '20 at 17:52
  • @CommonsWare Added the code. Thanks – Vijai Mar 29 '20 at 03:48
  • Um, I was looking for a bit more than that. In particular, show what you are doing with this `Uri`. For example, are you using it in an `Intent`? If so, show how you are setting up that `Intent` and what you are doing with the `Intent` in the end. – CommonsWare Mar 29 '20 at 11:31
  • The entire code has been added. It is used for a share intent. – Vijai Mar 29 '20 at 11:37
  • Add `intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)` after your second `putExtra()` call and before the `startActivity()` call. Also, replace `setType("*/*")` with a call to `setType()` where you pass in the actual MIME type -- it is *your* content, so *you* are responsible for telling other apps what the MIME type is. – CommonsWare Mar 29 '20 at 11:39
  • @CommonsWare Thanks, meme type seems a valid point. For granting URI permission, I fear it hasnt changed anything, I still get the exception, – Vijai Mar 29 '20 at 11:52
  • I can't explain the exception then, and I can't explain how you are able to share despite that exception. So, if it's working, I guess continue on, but this is rather strange. – CommonsWare Mar 29 '20 at 11:54
  • I've encountered this as well, on Android 12. I can see the error in logcat but I can proceed with emailing with attachment. – Alvin Dizon Aug 02 '22 at 03:09

0 Answers0