Search code examples
androidandroid-intentsecurityexception

Intent recieved from action send generates SecurityException on URI


I'm trying to accept images with my app to send them to a server. Setting up the intent filter was easy enough and that works like a charm. The thing i can't seem to figure out is the way i need to retrieve the file path from the uri in the intent.

I receive intents on my activity with this filter

<intent-filter>
   <action android:name="android.intent.action.SEND" />
   <action android:name="android.intent.action.SEND_MULTIPLE" />
   <category android:name="android.intent.category.DEFAULT" />
   <data android:mimeType="image/jpeg" />
   <data android:mimeType="image/png" />
   <data android:mimeType="image/gif" /> 
</intent-filter>

By trying to process the uri to get the file path I get this error by calling takePersistableUriPermission(uri, modeFlags):

java.lang.SecurityException: No persistable permission grants found for UID 10098 and Uri 0 @ content://media/external/images/media/124

The logic I use works for requesting images with an intent from inside my app, but receiving an intent from outside the app generates the error.

for Kitkat and above:

   final int flags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
   final String[] imageColumns = {MediaStore.Images.Media.DATA};
   getActivity().getContentResolver().takePersistableUriPermission(uri, flags);
   String id = uri.getLastPathSegment().split(":")[1]; 
   Cursor imageCursor = context.managedQuery(getStorageUri(), imageColumns, MediaStore.Images.Media._ID + "=" + id, null, null);

Solution

  • The thing i can't seem to figure out is the way i need to retrieve the file path from the uri in the intent.

    There is no "file path from the uri". A Uri is not a file.

    By trying to proses the uri to get the file path I get this error by calling takePersistableUriPermission(uri, modeFlags):

    That just means that you cannot use the Uri once your process ends.

    for Kitkat and above:

    ACTION_GET_CONTENT is not obligated to return you some Uri that can be processed using that code.

    To consume the content identified by the Uri, use openInputStream() on your ContentResolver.