从 Kitkat Gallery 发布图片

Issue Picking Image From Kitkat Gallery

我正在使用以下代码,它在每个 Lollipop、Marshmallow 等设备上都能完美运行。但是在有点旧的 KitKat 上,我遇到了以下崩溃,这似乎是导致此问题的路径无效或 null崩溃。

Fatal Exception: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=131294, result=-1, data=Intent { dat=content://media/external/images/media/3965 (has extras) }} to activity {com.yospace.b4umobile/com.yospace.b4umobile.NonLogedInScreen}: java.lang.NullPointerException
       at android.app.ActivityThread.deliverResults(ActivityThread.java:3641)
       at android.app.ActivityThread.handleSendResult(ActivityThread.java:3684)
       at android.app.ActivityThread.access00(ActivityThread.java:166)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:136)
       at android.app.ActivityThread.main(ActivityThread.java:5584)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
       at dalvik.system.NativeStart.main(NativeStart.java)
Caused by java.lang.NullPointerException
       at com.yospace.b4umobile.fragments.Signup.getResizedBitmap(Signup.java:934)
       at com.yospace.b4umobile.fragments.Signup.onSelectFromGalleryResult(Signup.java:873)
       at com.yospace.b4umobile.fragments.Signup.onActivityResult(Signup.java:752)
       at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:165)
       at android.app.Activity.dispatchActivityResult(Activity.java:5634)
       at android.app.ActivityThread.deliverResults(ActivityThread.java:3637)
       at android.app.ActivityThread.handleSendResult(ActivityThread.java:3684)
       at android.app.ActivityThread.access00(ActivityThread.java:166)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:136)
       at android.app.ActivityThread.main(ActivityThread.java:5584)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
       at dalvik.system.NativeStart.main(NativeStart.java)

这是我的代码:

private void galleryIntent() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);//
        startActivityForResult(Intent.createChooser(intent, "Select File"), CAMERA_CAPTURE_IMAGE_GALLERY_CODE);
    }


    @SuppressWarnings("deprecation")
    private void onSelectFromGalleryResult(Intent data) {

        Bitmap bm = null;
        if (data != null) {
            try {

                bm = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), data.getData());


                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getActivity().getContentResolver().query(
                        selectedImage, filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                filePath = cursor.getString(columnIndex);
                cursor.close();

                if (filePath == null)
                    setLolliPopPath(data);


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

             /*   storeImage(bm, IMAGE_DIRECTORY_NAME);

                filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myAppDir/" + FILE_NAME;*/
            }
        }

        //ivImage.setImageBitmap(bm);
if(bm!=null)
        profile_img.setImageBitmap(getResizedBitmap(bm, 250));
       else
            Toast.makeText(getContext(),"Issue with your device. Fail to access gallery",Toast.LENGTH_LONG).show();
    }
private void setLolliPopPath(Intent data) {

        String wholeID="";
        Uri selectedImage = data.getData();
        if(Build.VERSION.SDK_INT<=Build.VERSION_CODES.JELLY_BEAN_MR2){
            wholeID=getUriPreKitkat(selectedImage);
        }else {
            wholeID = DocumentsContract.getDocumentId(selectedImage);

        }

        // Split at colon, use second item in the array
        String id = wholeID.split(":")[1];

        String[] column = {MediaStore.Images.Media.DATA};

        // where id is equal to
        String sel = MediaStore.Images.Media._ID + "=?";

        Cursor cursor = getActivity().getContentResolver().
                query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        column, sel, new String[]{id}, null);


        int columnIndex = cursor.getColumnIndex(column[0]);

        if (cursor.moveToFirst()) {
            filePath = cursor.getString(columnIndex);
        }
        cursor.close();

    }

设备详细信息:

如有任何帮助,我们将不胜感激。

没有"Kitkat Gallery",你的代码跟"gallery"关系不大。

有大约 20 亿 Android 台设备,分布在大约 10,000 种设备型号中,它们本身跨越数百家制造商和(不幸的是)十几个版本的 Android。数百种支持 ACTION_GET_CONTENT 的不同应用程序将预装在这些设备上,另外还有数百种(如果不是数千种)可供用户从 Play 商店和其他应用程序分发渠道安装。

您假设每个 Uri 交付给 onActivityResult() 都为 MediaStore 所知。实际上,没有要求 Uri 交付给 onActivityResult()MediaStore.

有任何关系

例如,我可以编写一个支持 ACTION_GET_CONTENT 和 returns 的应用程序和 Uri 到我的应用程序通过 FileProvider 提供的图像,其中该图像存储在我的应用程序的内部存储部分中。 MediaStore 将对这张图片一无所知。 DocumentsContract.

也不会

Uri 是一个不透明的句柄,您不应该对 Uri 来自哪里做出任何假设.

使用 ContentResolveropenInputStream()BitmapFactory.decodeStream()Uri 标识的内容上获得 Bitmap。或者,更好的是,使用图像加载库(例如 Glide、Picasso),因为它们将在后台线程上完成这项工作,而您的代码在您执行所有这些操作时会冻结 UI I/O.