Android:将操作系统版本更新为5.1.1时图像裁剪后崩溃

Android:将操作系统版本更新为5.1.1时图像裁剪后崩溃,android,image,crop,android-camera-intent,Android,Image,Crop,Android Camera Intent,我将我的Nexus5Android操作系统版本更新为5.1.1,还更新了谷歌相机和谷歌照片应用程序。在此之后,当我尝试捕获图像并对其进行裁剪时,我的应用程序会崩溃,并出现以下错误: FATAL EXCEPTION: main Process: com.app.test, PID: 4857 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=In

我将我的Nexus5Android操作系统版本更新为5.1.1,还更新了谷歌相机和谷歌照片应用程序。在此之后,当我尝试捕获图像并对其进行裁剪时,我的应用程序会崩溃,并出现以下错误:

FATAL EXCEPTION: main
Process: com.app.test, PID: 4857
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { typ=image/jpeg }} to activity {com.app.test/com.app.test.newActivity.activities.TestActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)' on a null object reference
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3574)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)
        at android.app.ActivityThread.access$1300(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)' on a null object reference
        at com.app.test.newActivity.activities.TestActivity.onActivityResult(TestActivity.java:127)
        at android.app.Activity.dispatchActivityResult(Activity.java:6192)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3570)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)
            at android.app.ActivityThread.access$1300(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
以前它工作得很好。我使用的代码如下:

图像捕获代码:

try {
    Intent imageCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (imageCapture.resolveActivity(getContext().getPackageManager()) != null) {
        imageCapture.putExtra(MediaStore.EXTRA_OUTPUT,  Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + Constants.image_path)));
        startActivityForResult(imageCapture, Constants.CAMERA_IMAGE_CAPTURE);
    }
} catch (ActivityNotFoundException anfe) {
    Toast.makeText(getContext(), "device doesn't support capturing images!", Toast.LENGTH_SHORT).show();
}
图像裁剪代码

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (resultCode == RESULT_OK) {
            if (requestCode == CAMERA_IAMGE_CROP) {
                Bundle extras = intent.getExtras();//intent.getExtras() is always returns NULL here         
                Bitmap thePic = extras.getParcelable("data");
                //setImageOnImageView(thePic);
            } else if (requestCode == Constants.CAMERA_IMAGE_CAPTURE)) {
                processCapturedImage();
            }
        }
    }

    private void processCapturedImage() {
        try {
            String path = Environment.getExternalStorageDirectory().getAbsolutePath() + Constants.image_path;
            File file = new File(path);
            if (file.exists()) {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inPreferredConfig = Bitmap.Config.RGB_565;
                Bitmap bm = BitmapFactory.decodeFile(path, options);
                int rotate = AndroidUtils.getRotateValue(file.getAbsolutePath());
                if (rotate != 0) {
                    Debug.print("Profile pic rotation value is not 0.");
                    /****** Image rotation ****/
                    Matrix matrix = new Matrix();
                    matrix.postRotate(rotate);
                    bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
                }
                picUri = getImageUri(getApplicationContext(), bm);
                performCropAction();
            } else {
                Tools.showToast(EditProfileActivity.this, "Error occurred, please try again.");
            }
        } catch (Exception e) {
            Debug.printException(e);
        }
    }

    private void performCropAction() {
        try {
            Intent cropAction = new Intent("com.android.camera.action.CROP");
            cropAction.setDataAndType(picUri, "image/*");
            cropAction.putExtra("crop", "true");
            cropAction.putExtra("aspectX", 1);
            cropAction.putExtra("aspectY", 1);
            cropAction.putExtra("outputX", AS.getInPixels(100));
            cropAction.putExtra("outputY", AS.getInPixels(100));
            cropAction.putExtra("return-data", true);
            startActivityForResult(cropAction, CAMERA_IAMGE_CROP);
        } 
        catch (ActivityNotFoundException anfe) {
            Toast.makeText(this, "your device doesn't support the crop action!", Toast.LENGTH_SHORT).show();
        }
    }
如你所见, Bundle extras=intent.getExtras() 此处的intent.getExtras()始终返回NULL。

非常感谢您的帮助!
谢谢。

我也有同样的问题。新的裁剪操作未使用ActivityResult()方法。分辨率:将文件复制到要保存裁剪图像的文件夹中,使用先前复制的文件启动“com.android.camera.action.CROP”。当用户在裁剪后单击“保存”按钮时,新复制的文件将替换为裁剪后的图像。如果你有任何问题,请问我。

@nikash 解决方案一:如果您只想使用标准android摄像头的裁剪活动,您应该知道新的(android 5.1.1)摄像头的裁剪活动不会为onActivityResult()方法返回任何内容。它只是裁剪你提供的图像。如果您将为裁剪活动提供原始图像-它将用新裁剪的图像替换原始图像。它不能单独保存它。您可以创建原始图像的副本(不要将此副本放在应用程序的“数据”文件夹中,因为相机的“裁剪”活动无权处理活动文件夹中的任何文件)。之后,可以将复制图像的新链接发送到相机的裁剪活动。然后,在裁剪之后,您可以将裁剪(复制之前)的图像移动到所需的任何文件夹

解决方案二:比第一个更好。您可以使用此图像裁剪器:

第二种解决方案比第一种好。易于使用,易于实现,而且您不关心其他设备的bug。它可以在任何地方工作,而无需标准相机的裁剪活动。您的应用程序可以是API7或更高版本。别忘了把“compile'com.edmodo:crapper:1.0.1'”放在gradle文件中。编译。好好享受

用法示例: 主要活动:

Intent cropIntent = new Intent(Main.this, CropActivity.class);
cropIntent.putExtra("from", input.getAbsolutePath());
cropIntent.putExtra("to", output.getAbsolutePath());
startActivityForResult(cropIntent, CROP_IMAGE);
Bitmap bmp = BitmapFactory.decodeFile(new File(getIntent().getExtras().getString("from")).getAbsolutePath());
cropImageView.setImageBitmap(bmp);
saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getIntent().putExtra("bitmap", cropImageView.getCroppedImage());
                setResult(RESULT_OK, getIntent());
                finish();
            }
        });
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK && data != null && requestCode == CROP_IMAGE)
    {
        croppedBitmap.setImageBitmap((Bitmap) data.getParcelableExtra("bitmap"));
    }
}
作物活动:

Intent cropIntent = new Intent(Main.this, CropActivity.class);
cropIntent.putExtra("from", input.getAbsolutePath());
cropIntent.putExtra("to", output.getAbsolutePath());
startActivityForResult(cropIntent, CROP_IMAGE);
Bitmap bmp = BitmapFactory.decodeFile(new File(getIntent().getExtras().getString("from")).getAbsolutePath());
cropImageView.setImageBitmap(bmp);
saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getIntent().putExtra("bitmap", cropImageView.getCroppedImage());
                setResult(RESULT_OK, getIntent());
                finish();
            }
        });
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK && data != null && requestCode == CROP_IMAGE)
    {
        croppedBitmap.setImageBitmap((Bitmap) data.getParcelableExtra("bitmap"));
    }
}
在主活动中收到后:

Intent cropIntent = new Intent(Main.this, CropActivity.class);
cropIntent.putExtra("from", input.getAbsolutePath());
cropIntent.putExtra("to", output.getAbsolutePath());
startActivityForResult(cropIntent, CROP_IMAGE);
Bitmap bmp = BitmapFactory.decodeFile(new File(getIntent().getExtras().getString("from")).getAbsolutePath());
cropImageView.setImageBitmap(bmp);
saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getIntent().putExtra("bitmap", cropImageView.getCroppedImage());
                setResult(RESULT_OK, getIntent());
                finish();
            }
        });
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK && data != null && requestCode == CROP_IMAGE)
    {
        croppedBitmap.setImageBitmap((Bitmap) data.getParcelableExtra("bitmap"));
    }
}
裁剪图像活动布局应包含:

<com.edmodo.cropper.CropImageView
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:id="@+id/CropImageView"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    custom:aspectRatioX="5" />


在这里,您可以找到有关用法的更多信息:

以上android 5.0裁剪函数在onActivityResult中返回URI,因此请相应地使用手机版本处理它

Bitmap selectedBitmap;
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Bundle extras = data.getExtras();
selectedBitmap = extras.getParcelable("data");
}
else{
Uri uri = data.getData(); 
selectedBitmap=MediaStore.Images.Media.getBitmap(this.getContentResolver(),uri);
}
位图选择位图;
if(Build.VERSION.SDK\u INT

希望这有帮助

Android没有
裁剪
意图
。Android有很多可用的功能;请使用一个。但相同的代码在5.1.1以下有效。在5.1.1中,应用程序意外崩溃。{java.lang.RuntimeException:未能将结果ResultInfo{who=null,request=1,result=0,data=null}传递到活动{camera.test.demo/camera.test.demo.SimpleCameraGalleryDemo}:java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“android.os.Bundle android.content.Intent.getExtras()”“但同样的代码在5.1.1以下也适用“--安卓设备有几千种型号。我很有信心,你没有测试过所有的,甚至很多。最有可能的是,您只测试了其中一个或两个。草率的开发人员假定所有设备都支持未记录、不受支持和非官方的
Intent
操作。有才华的开发人员使用库。下面链接中的代码也不适用于OS 5.1.1。但低于5.1.1,工作正常。我认为这是操作系统特有的问题!!!你能再解释一下你的解决方案吗?@nilkash,全部完成。你可以在下面查看新评论。