java.lang.SecurityException:权限拒绝:打开提供程序 com.google.android.apps.photos.content.GooglePhotosImageProvider
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21082708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.SecurityException: Permission Denial: opening provider com.google.android.apps.photos.content.GooglePhotosImageProvider
提问by Snake
All of the sudden, I started getting the following exception from devices running android 4.3 and above
突然之间,我开始从运行 android 4.3 及更高版本的设备中收到以下异常
java.lang.SecurityException: Permission Denial: opening provider com.google.android.apps.photos.content.GooglePhotosImageProvider from ProcessRecord{454ca9d0 5914:com.propertymanager/u0a10231} (pid=5914, uid=10231) requires com.google.android.apps.photos.permission.GOOGLE_PHOTOS or com.google.android.apps.photos.permission.GOOGLE_PHOTOS
at android.os.Parcel.readException(Parcel.java:1431)
at android.os.Parcel.readException(Parcel.java:1385)
at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:2896)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:4755)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2480)
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1152)
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:759)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:665)
at android.content.ContentResolver.openInputStream(ContentResolver.java:500)
at com.myapp.xxxx.determineCorrectScale(SourceFile:148)
My The code causing it is
我的导致它的代码是
public static int determineCorrectScale(Context con, Uri imagUri) {
int scale = 1;
InputStream imageStream = null;
imageStream = con.getContentResolver().openInputStream(imagUri);
.
.
.
}
Any help??
有什么帮助吗??
EDIT: This is how I let the user pick a picture before calling the above method
编辑:这就是我在调用上述方法之前让用户选择图片的方式
Intent choosePictureIntent = new Intent(Intent.ACTION_PICK, Images.Media.INTERNAL_CONTENT_URI);
tartActivityForResult(choosePictureIntent, REQUEST_CHOOSE_IMAGE);
Here is the OnActivityResult:
这是 OnActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_CHOOSE_IMAGE:
Uri selectedImage = intent.getData();
try {
int scale = CommonFunc.determineCorrectScale(this, selectedImage);
回答by Emanuel Moecklin
While adding the permission works for this particular content provider it won't work if your data is delivered by other content providers (on Android there's no guarantee you'll get the data by a specific content provider unless you explicitly access that content provider).
虽然添加权限适用于这个特定的内容提供者,但如果你的数据是由其他内容提供者提供的,它就不起作用(在 Android 上,除非你明确访问该内容提供者,否则不能保证你会从特定的内容提供者获得数据)。
The "final" solution to this problem can be found here:
可以在此处找到此问题的“最终”解决方案:
Getting access with temporary permissions
You can access data in a content provider, even if you don't have the proper access permissions, by sending an intent to an application that does have the permissions and receiving back a result intent containing "URI" permissions. These are permissions for a specific content URI that last until the activity that receives them is finished.
使用临时权限获取访问权限
即使您没有适当的访问权限,您也可以通过向具有权限的应用程序发送意图并接收包含“URI”权限的结果意图来访问内容提供者中的数据。这些是特定内容 URI 的权限,持续到接收它们的活动完成。
http://developer.android.com/guide/topics/providers/content-provider-basics.html
http://developer.android.com/guide/topics/providers/content-provider-basics.html
Since version 4.3 Android checks whether the receiving Activity is still running and if not throws the SecurityException. The determineCorrectScale is a static method so I assume it's at least sometimes called outside the Activity life cycle. To fix this once and for all you need to retrieve the data from the content provider while the Activity is running. I don't know the requirements for your app but if there's no heavy lifting involved (like copying images from the content provider) then just do it on the ui thread. If heavy lifting is needed then use an AsyncTask started by the Activity but then you have to make sure the Activity doesn't finish before the data has been retrieved (which might be tricky).
从 4.3 版开始,Android 会检查接收 Activity 是否仍在运行,如果没有则抛出 SecurityException。determineCorrectScale 是一个静态方法,所以我认为它至少有时会在 Activity 生命周期之外被调用。要一劳永逸地解决此问题,您需要在 Activity 运行时从内容提供程序中检索数据。我不知道您的应用程序的要求,但如果不涉及繁重的工作(例如从内容提供程序复制图像),那么只需在 ui 线程上执行即可。如果需要繁重的工作,则使用由 Activity 启动的 AsyncTask,但是您必须确保在检索数据之前 Activity 没有完成(这可能很棘手)。
I didn't just add that answer because I think it's the correct one but because other developers might not be aware of this change introduced in 4.3/4.4 and might run into the same issue with other content providers.
我添加该答案不仅仅是因为我认为它是正确的,而是因为其他开发人员可能没有意识到 4.3/4.4 中引入的这一更改,并且可能会遇到与其他内容提供商相同的问题。
回答by Coderji
you need to add this permission to your manifest:
您需要将此权限添加到您的清单中:
<uses-permission android:name="com.google.android.apps.photos.permission.GOOGLE_PHOTOS"/>
hope this works for you. please give me a feedback
希望这对你有用。请给我一个反馈