问题从Kitkat画廊挑选图像

时间:2017-09-15 15:08:51

标签: android gallery android-4.4-kitkat

我正在使用以下代码,这些代码在每个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.access$1300(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.access$1300(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();

    }

设备详细信息: enter image description here

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

没有&#34; Kitkat Gallery&#34;,您的代码与&#34;画廊&#34;没什么关系。

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

您认为传递给Uri的每onActivityResult()MediaStore都已为Uri所知。实际上,onActivityResult()传递给MediaStore的{​​{1}}与ACTION_GET_CONTENT没有任何关系。

例如,我可以编写一个支持Uri的应用,并将FileProvider返回到我的应用通过MediaStore提供的图片,其中该图片存储在我的应用中&# 39;内部存储的一部分。 DocumentsContract对此图片一无所知。也不会Uri

Uri是一个不透明的句柄,您不应该对ContentResolver来自的位置做任何假设。

使用openInputStream()BitmapFactory.decodeStream()Bitmap获取Uri标识的内容Sub AddText() Dim sInput as String sInput = Application.inputbox("Enter Item Number", "1") Worksheets("Sheet1").Range("S1").Value = sInput Worksheets("Sheet2").Range("S1").Value = sInput End Sub 。或者,更好的是,使用图像加载库(例如,Glide,Picasso),因为他们将在后台线程上工作,而您的代码在执行所有这些I / O时冻结UI。