从操作发送中收到的意图在 URI 上生成 SecurityException

Intent recieved from action send generates SecurityException on URI

我正在尝试使用我的应用程序接受图像以将它们发送到服务器。设置 Intent 过滤器非常简单,而且效果很好。我似乎无法弄清楚的是我需要从意图中的 uri 检索文件路径的方式。

我的 activity 使用此过滤器

收到意向
<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>

通过尝试处理 uri 以获取文件路径,我通过调用 takePersistableUriPermission(uri, modeFlags):

得到了这个错误
java.lang.SecurityException: No persistable permission grants found for UID 10098 and Uri 0 @ content://media/external/images/media/124

我使用的逻辑适用于从我的应用程序内部请求带有意图的图像,但从应用程序外部接收意图会产生错误。

Kitkat 及以上:

   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);

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.

没有"file path from the uri"。 Uri 不是文件。

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

这只是意味着您无法在进程结束后使用 Uri

for Kitkat and above:

ACTION_GET_CONTENT 没有义务 return 你可以使用该代码处理一些 Uri

消费由 Uriuse openInputStream() on your ContentResolver.

标识的内容