我的应用程序成功地从相机拍照,然后将用户重定向到一个弹出列表,其中包含可以裁剪照片的可用应用程序,如下所示:
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setType("image/*");
List<ResolveInfo> list = mActivity.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
当用户选择 Google Photos 选项时,会出现以下异常:
Fatal Exception: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=66990, result=-1, data=Intent { dat=content://com.google.android.apps.photos.contentprovider/0/1/content://media/external/images/media/6519/ORIGINAL/NONE/2122203960 flg=0x1 launchParam=MultiScreenLaunchParams { mDisplayId=0 mFlags=0 } clip={text/uri-list U:content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F6519/ORIGINAL/NONE/2122203960} }} to activity {com.turkcell.android.ccsimobile/com.turkcell.android.ccsimobile.HomeActivity}: java.lang.SecurityException: Uid 10050 does not have permission to uri 0 @ content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F6519/ORIGINAL/NONE/2122203960
at android.app.ActivityThread.deliverResults(ActivityThread.java:4472)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4515)
at android.app.ActivityThread.-wrap22(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1687)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by java.lang.SecurityException: Uid 10050 does not have permission to uri 0 @ content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F6519/ORIGINAL/NONE/2122203960
at android.os.Parcel.readException(Parcel.java:1693)
at android.os.Parcel.readException(Parcel.java:1646)
at android.app.ActivityManagerProxy.grantUriPermission(ActivityManagerNative.java:5763)
at android.app.ContextImpl.grantUriPermission(ContextImpl.java:1744)
at android.content.ContextWrapper.grantUriPermission(ContextWrapper.java:764)
在这一行:
getContext().grantUriPermission(packageName, mSelectedImage, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
此行在循环中用于为显示裁剪选项的每个可用应用程序授予权限:
for (ResolveInfo res : list) {
String packageName = res.activityInfo.packageName;
getContext().grantUriPermission(packageName, mSelectedImage, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
...
...
}
异常说没有权限,但我已经使用了 grantPermission() 方法。
注意: mSelectedImage 是相机拍摄的照片的 uri。