1

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);
SjoerdvGestel
  • 391
  • 2
  • 14

1 Answers1

1

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I guess you misunderstood my question. for one, i am aware that the URI does not contain the file path, and therefore i need to process it to get the file path. I'm using an intent filter here that will receive an intent from another app that sends that content, not `ACTION_GET_CONTENT` with start activity for result. – SjoerdvGestel Apr 29 '16 at 12:27
  • 1
    @SjoerdvGestel: "and therefore i need to process it to get the file path" -- you cannot "process it to get the file path", other than perhaps using `openInputStream()` to copy the content to some file that you control. There is no requirement for the user to pick a piece of content that you can access via the filesystem. The content could be on internal storage of another app, or on removable storage, or in a BLOB column of another app's database, or in "the cloud" that needs to be downloaded by the other app. – CommonsWare Apr 29 '16 at 12:30
  • 1
    @SjoerdvGestel: Similarly, there is no requirement for a third-party app to grant you persistable permissions to that `Uri`. That is up to the other app to offer; you cannot force developers to offer it, short of using heavy weaponry. – CommonsWare Apr 29 '16 at 12:32
  • thanks! i've compaired the uri's from booth inside my app and from the intent filter and there differen. thats the issue i thought booth would be the same. – SjoerdvGestel Apr 29 '16 at 12:35
  • yep getActivity().getContentResolver().takePersistableUriPermission(uri, flags); is not needed at all – SjoerdvGestel Apr 29 '16 at 12:43