gpt4 book ai didi

android - 努力获得 'run-time' Android WRITE_EXTERNAL_STORAGE 权限

转载 作者:行者123 更新时间:2023-11-29 02:23:51 25 4
gpt4 key购买 nike

我有一个 fragment 。在此 fragment 中,我使用 onActivityResult() 在用户选择照片并将该照片导入我的应用程序时捕获 Intent :

Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CAMERA) {
if (resultCode == Activity.RESULT_OK) {
Uri takenPhotoUri = ImageUtils.getInstance().getPhotoFileUri(getContext(), imageFileName);
actionsListener.onSignalPhotoSelected(takenPhotoUri.getPath());
}
}

if (requestCode == REQUEST_GALLERY && resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {

Uri fileUri = data.getData();
String path = null;
File photoFile = new File(fileUri.toString());

// Differentiate between SDK versions
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

// Code for Android 6 and > goes here
if(getActivity().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
//showPermissionDialog(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE_FOR_CLOUDE);
ActivityCompat.requestPermissions(getActivity(), new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_EXTERNAL_STORAGE_FOR_CLOUDE);
}
try {
ParcelFileDescriptor parcelFileDesc = getActivity().getContentResolver().openFileDescriptor(fileUri, "r");
FileDescriptor fileDesc = parcelFileDesc.getFileDescriptor();
Bitmap photo = BitmapFactory.decodeFileDescriptor(fileDesc);
path = MediaStore.Images.Media.insertImage(getContext().getContentResolver(), photo, "temp", null);
parcelFileDesc.close();

// DRY!!
photoFile = ImageUtils.getInstance().getFromMediaUri(getContext(), getContext().getContentResolver(), Uri.parse(path));

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
else {
// DRY!!
photoFile = ImageUtils.getInstance().getFromMediaUri(getContext(), getContext().getContentResolver(), data.getData());
}
if(photoFile != null) {
actionsListener.onSignalPhotoSelected(Uri.fromFile(photoFile).getPath());
}
}

if (requestCode == REQUEST_SIGNAL_DETAILS) {
if (resultCode == Activity.RESULT_OK) {
Signal signal = data.getParcelableExtra("signal");
if (signal != null) {
actionsListener.onSignalStatusUpdated(signal);
}
}
}
}

我的问题是:当用户从 Google 相册中选择一张照片(一张只存在于云端并且本地下载的照片)时,我的应用程序崩溃并出现以下错误:

E/MediaStore: Failed to insert image java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=9851, uid=10091 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission() at android.os.Parcel.readException(Parcel.java:1943) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135) at android.content.ContentProviderProxy.insert(ContentProviderNative.java:476) at android.content.ContentResolver.insert(ContentResolver.java:1539) at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:984) at org.helpapaw.helpapaw.signalsmap.SignalsMapFragment.onActivityResult(SignalsMapFragment.java:738) at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:151) at android.app.Activity.dispatchActivityResult(Activity.java:7235) at android.app.ActivityThread.deliverResults(ActivityThread.java:4320) at android.app.ActivityThread.handleSendResult(ActivityThread.java:4367) at android.app.ActivityThread.-wrap19(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1649) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main Process: org.helpapaw.helpapaw, PID: 9851 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=196611, result=-1, data=Intent { dat=content://com.google.android.apps.photos.contentprovider/0/1/mediakey:/local%3Aabc88a06-d518-4fae-b85d-73f727595a90/ORIGINAL/NONE/1144559444 flg=0x1 clip={text/uri-list U:content://com.google.android.apps.photos.contentprovider/0/1/mediakey%3A%2Flocal%253Aabc88a06-d518-4fae-b85d-73f727595a90/ORIGINAL/NONE/1144559444} }} to activity {org.helpapaw.helpapaw/org.helpapaw.helpapaw.signalsmap.SignalsMapActivity}: java.lang.NullPointerException: uriString at android.app.ActivityThread.deliverResults(ActivityThread.java:4324) at android.app.ActivityThread.handleSendResult(ActivityThread.java:4367) at android.app.ActivityThread.-wrap19(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1649) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) Caused by: java.lang.NullPointerException: uriString at android.net.Uri$StringUri.(Uri.java:476) at android.net.Uri$StringUri.(Unknown Source:0) at android.net.Uri.parse(Uri.java:438) at org.helpapaw.helpapaw.signalsmap.SignalsMapFragment.onActivityResult(SignalsMapFragment.java:742) at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:151) at android.app.Activity.dispatchActivityResult(Activity.java:7235) at android.app.ActivityThread.deliverResults(ActivityThread.java:4320) at android.app.ActivityThread.handleSendResult(ActivityThread.java:4367)  at android.app.ActivityThread.-wrap19(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1649)  at android.os.Handler.dispatchMessage(Handler.java:105)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6541)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

该错误的第一行让我想到了 WRITE_TO_EXTERNAL 权限错误:

E/MediaStore: Failed to insert image java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=9851, uid=10091 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()

所以我检查以确保此 API 版本需要“运行时”权限(如您在上面的代码中所见),以及以下行:

ActivityCompat.requestPermissions(getActivity(), new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_EXTERNAL_STORAGE_FOR_CLOUDE);

被击中,但屏幕上没有出现确认对话框。另一个有趣的事实是,该错误仅在我第一次运行该应用程序时发生(在 API 8.0 上)并且在 5.1、6.0、7.1 和 8.1 上运行良好但在 8.0(模拟器)上崩溃。我错过了什么?

最佳答案

对于任何遇到类似问题的人;关键是使用(在我的例子中)Fragment.requestPermissions(String[] permissions, int requestCode); 方法一次请求所有必要的权限。此方法允许将一组权限传递给它的第一个参数:

    String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(permissions, READ_WRITE_EXTERNAL_STORAGE_FOR_GALLERY);
}
  • 我希望我知道的是:

    一旦一个新的 Activity 被 Intent 调用,无论是相机还是照片库;试图向用户询问权限不再有效;意思是——一旦相机或照片库打开,我们就失去了我们自己的应用程序的上下文,因此我们不能再提示用户权限。最重要的是,在用户离开我们的应用程序之前请求所有权限(在我的例子中,需要使用数组来请求读取和写入外部存储的权限。请求所有必要的权限在用户离开当前上下文之前的同一时间,然后通过 Intent 重定向到新的 Activity。

我希望这对某人有帮助。

关于android - 努力获得 'run-time' Android WRITE_EXTERNAL_STORAGE 权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53437311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com