突然之间,我开始从运行 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)
我的导致它的代码是
public static int determineCorrectScale(Context con, Uri imagUri) {
int scale = 1;
InputStream imageStream = null;
imageStream = con.getContentResolver().openInputStream(imagUri);
.
.
.
}
有什么帮助吗??
编辑:这就是我在调用上述方法之前让用户选择图片的方式
Intent choosePictureIntent = new Intent(Intent.ACTION_PICK, Images.Media.INTERNAL_CONTENT_URI);
tartActivityForResult(choosePictureIntent, REQUEST_CHOOSE_IMAGE);
这是 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();
thử {
int scale = CommonFunc.determineCorrectScale(this, selectedImage);
虽然添加权限适用于该特定内容提供者,但如果您的数据由其他内容提供者提供,则该权限将不起作用(在 Android 上,除非您明确访问该内容,否则无法保证您会通过特定内容提供者获取数据提供者)。
这个问题的“最终”解决方案可以在这里找到:
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.
http://developer.android.com/guide/topics/providers/content-provider-basics.html
从 4.3 版开始,Android 会检查接收的 Activity 是否仍在运行,如果没有则抛出 SecurityException。 determineCorrectScale 是一个静态方法,所以我认为它至少有时会在 Activity 生命周期之外调用。要一劳永逸地解决这个问题,您需要在 Activity 运行时从内容提供者检索数据。我不知道您的应用程序的要求,但如果不涉及繁重的工作(例如从内容提供商复制图像),那么只需在 ui 线程上执行。如果需要繁重的工作,则使用由 Activity 启动的 AsyncTask,但您必须确保 Activity 在检索数据之前没有完成(这可能很棘手)。
我不只是添加那个答案,因为我认为它是正确的,而是因为其他开发人员可能不知道 4.3/4.4 中引入的这一更改,并且可能会遇到与其他内容提供商相同的问题。
Tôi là một lập trình viên xuất sắc, rất giỏi!