android - 无法从 android 7.1 及以上设备的图库中选择图像

标签 android

我正在尝试从图库中选择图像,它在 android 7.1 以下运行良好,但在 android 7.1 中它对我不起作用。一样的怎么修。 这是我试过的代码:

public void LaunchGalleryView() {
        mFileUri = null;
        Intent intent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        intent.setType("image/*");
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_CODE_GALLERY);
    }

这是 onActivityResult() 中的代码:

 case REQUEST_CODE_GALLERY:
                if (data == null) {
                    return;
                }
                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();

                File file = new File(picturePath);
                mFileUri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", file);
                performCrop();
                break;

如何解决同样的问题,请帮助我。

这是错误日志:

12-28 05:13:30.085 13533-13533/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.welldoc.platform.android.cyan, PID: 13533 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.google.android.apps.photos.contentprovider/0/1/mediakey:/local%3Aefc38517-5fd9-4b57-afbf-6329e508fb66/ORIGINAL/NONE/1646850308 flg=0x1 clip={text/uri-list U:content://com.google.android.apps.photos.contentprovider/0/1/mediakey%3A%2Flocal%253Aefc38517-5fd9-4b57-afbf-6329e508fb66/ORIGINAL/NONE/1646850308} }} to activity {com.welldoc.platform.android.cyan/com.welldoc.platform.android.ui.profile.ProfileDetailsEditActivity}: java.lang.NullPointerException 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 at java.io.File.(File.java:282) at com.welldoc.platform.android.ui.profile.ProfileDetailsEditActivity.onActivityResult(ProfileDetailsEditActivity.java:1059) 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)

最佳答案

1. 将图片路径与其他手机图片选择的路径进行比较,有时路径格式可能不同,所以会得到null。

关于android - 无法从 android 7.1 及以上设备的图库中选择图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48005463/

相关文章:

java - 使用 Display.getRotation() 进行屏幕旋转

Android Java : Fatal exception main nullpointer. 我没有明确定义的线程。它说它无法实例化 Activity

android - 如何知道何时在自定义 RecyclerView 中加载数据?

Android Studio 调试停止工作(OSx/Samsung Galaxy S5)

android - Android在YouTubeAndroidPlayerAPI中出现问题。

java - RGB 像素阵列只有一维而不是 vector 中的 3

java - 如何让通知播放声音?

Android aChartEngine 条形图

Android Make Disappear 或去除 map 上的蓝点 v2

android - ionic ( Cordova ): Gradle: more than one library with package name 'com.google.android.gms'