我将我的nexus 5 Android操作系统版本更新为5.1.1,并更新了Google相机和Google相册应用程序。在此之后,当我尝试捕获图像并裁剪它时,我的应用程序崩溃并出现以下错误:
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。
非常感谢任何帮助! 感谢。
答案 0 :(得分:6)
上面的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);
}
希望这有帮助!
答案 1 :(得分:2)
我有同样的问题。新的CROP操作不使用onActivityResult()方法。解决方法:将文件复制到要保存裁剪图像的文件夹,然后启动&#34; com.android.camera.action.CROP&#34;与先前复制的文件。当用户点击&#34;保存&#34;裁剪后按钮 - 新复制的文件将替换为裁剪图像。问你是否有任何问题。
答案 2 :(得分:2)
@nikash 解决方案一:如果您只想使用标准的Android Camera的裁剪活动,您应该知道新的(Android 5.1.1)相机的裁剪活动不会为onActivityResult()方法返回任何内容。它只会裁剪您提供的图像。如果您将使用原始图像提供裁剪活动 - 它将使用新裁剪的图像替换原始图像。它无法单独保存。您可以创建原始图片的副本(不要将此副本放入您的应用程序的数据文件夹,因为相机的裁剪活动无权使用您活动中的任何文件&#39; s文件夹)。之后,您可以向相机的裁剪活动发送复制图像的新链接。然后,在裁剪之后,您可以将裁剪(复印前)图像移动到所需的任何文件夹。
第二个解决方案:比第一个更好。您可以使用此图像裁剪器: https://android-arsenal.com/details/1/207
第二种解决方案比第一种更好。易于使用,易于实施,您不必关心其他设备错误。它可以在任何地方使用,无需标准相机的裁剪活动。您的申请可能是API7及以上。别忘了把#&#34;编译&#39; com.edmodo:cropper:1.0.1&#39;&#34;到您的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"));
}
}
裁剪图像活动布局应包含:
<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" />
您可以在此处找到有关使用情况的更多信息:https://github.com/edmodo/cropper/wiki