传递结果失败ResultInfo - 从库中获取图像

时间:2014-09-30 15:47:10

标签: android photo-gallery onactivityresult

这个问题已被多次询问,但似乎没有人回答我的问题。我有一个允许用户使用的应用程序。拍照并上传或b。从画廊中选择。

我一直在三台设备samsung s4 / s3和LG nexus 4上进行测试。它工作正常,但无法崩溃。

但是,我收到了关于摩托车摩托车坠毁事故的通知。

这是我的代码:

private void launchImageGallery(){
        //Toast.makeText(getActivity(), "Gallery Launched", Toast.LENGTH_LONG).show();

        Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(gallery, IMAGE_PICKER_SELECT);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == IMAGE_PICKER_SELECT && resultCode == Activity.RESULT_OK) {

            Uri selectedImageUri = data.getData();
            String imagePath = getPath(selectedImageUri);

            BitmapFactory.Options options = null;
            options = new BitmapFactory.Options();
            options.inSampleSize = 2;
            Matrix rotationMatrix = new Matrix();
            rotationMatrix.postRotate(90.0f);
            Bitmap bitty = decodeSampledBitmapFromPath(imagePath, 300, 300);
            Bitmap rotatedImage = Bitmap.createBitmap(bitty, 0, 0, bitty.getWidth(), bitty.getHeight(), rotationMatrix, true);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            fromGallery = true;
            BitmapDrawable bmp = null;
            Log.i("GALLERY BITMAP SIZE: ", String.valueOf((sizeOfBitmap(bitty))/1000)+ " kb");

//            normal portrait: 0
//            normal landscape: 90
//            upside-down portrait: 180
//            upside-down landscape: 270
//            image not found: -1

            if(getOrientation(getActivity(), selectedImageUri)!=90){
                bmp = new BitmapDrawable(getResources(),bitty);
                //galleryImage.setImageBitmap(bitty);
                bitty.compress(Bitmap.CompressFormat.PNG, 100, stream);
            }else{
                bmp = new BitmapDrawable(getResources(),rotatedImage);
                //galleryImage.setImageBitmap(rotatedImage);
                rotatedImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
            }

            if(Build.VERSION.SDK_INT>14){
                galleryImage.setBackground(bmp);
            }else{
                galleryImage.setBackgroundDrawable(bmp);
            }

            snapPic.setText(getString(R.string.rate_this_beer));
            image = stream.toByteArray();
            beerDetailsAnimate();
        }
    }
    public String getPath(Uri uri) {
        // just some safety built in
        if( uri == null ) {
            // TODO perform some logging or show user feedback
            return null;
        }
        // try to retrieve the image from the media store first
        // this will only work for images selected from gallery
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = getActivity().getContentResolver().query(uri, projection, null, null, null);
        if( cursor != null ){
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        // this is our fallback here
        return uri.getPath();
    }

我从崩溃中得到的日志是:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=132071, result=-1, data=Intent { dat=content://com.google.android.apps.photos.content/0/https://lh6.googleusercontent.com/poyCKb42_JLYFjZbndpgDzRBr7gEdfX8hNc4Dy6utJs=s0-d flg=0x1 }} to activity {com.purpledecks.bottomsup/com.purpledecks.bottomsup.activities.MainActivity}: java.lang.NullPointerException
       at android.app.ActivityThread.deliverResults(ActivityThread.java:3432)
       at android.app.ActivityThread.handleSendResult(ActivityThread.java:3475)
       at android.app.ActivityThread.access$1300(ActivityThread.java:139)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:136)
       at android.app.ActivityThread.main(ActivityThread.java:5086)
       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:785)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
       at dalvik.system.NativeStart.main(NativeStart.java)
Caused by: java.lang.NullPointerException
       at com.purpledecks.bottomsup.fragments.Beer_Camera_fragment.onActivityResult(Beer_Camera_fragment.java:1077)
       at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:167)
       at android.app.Activity.dispatchActivityResult(Activity.java:5446)
       at android.app.ActivityThread.deliverResults(ActivityThread.java:3428)
       at android.app.ActivityThread.handleSendResult(ActivityThread.java:3475)
       at android.app.ActivityThread.access$1300(ActivityThread.java:139)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:136)
       at android.app.ActivityThread.main(ActivityThread.java:5086)
       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:785)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
       at dalvik.system.NativeStart.main(NativeStart.java)

很遗憾,我无法在此设备上进行测试,所以我不确定哪里出错了,有人能看到这个问题吗?

0 个答案:

没有答案