所以直到昨天我把手机升级到 5.0.0 并在 5.0.0 模拟器中测试应用程序时,这一直很可爱。基本上目的是允许用户拍摄照片,返回照片,打开照片以允许裁剪,然后返回照片并将其设置为图像视图。
以前一切正常,但现在 onActivityResult 中的意图为空。
代码如下:
public void takeDisplayPicture(View view) {
final String TAKE_DISPLAY_PICTURE = "Take Display Picture";
Log.d(TAKE_DISPLAY_PICTURE, "Clicked");
try {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_CAPTURE);
} catch (ActivityNotFoundException e) {
String msg = "Your device doesn't support a camera!";
Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
toast.show();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
final String RETURN_DISPLAY_PICTURE = "Return Display Picture";
Log.d(RETURN_DISPLAY_PICTURE, "Returned");
if (resultCode == RESULT_OK && requestCode == CAMERA_CAPTURE) {
picUri = data.getData();
performCrop(picUri);
} else if (requestCode == PIC_CROP) {
Bundle extras = data.getExtras();
bitmap = extras.getParcelable("data");
displayPicture.setImageBitmap(bitmap);
}
}
private void performCrop(Uri picUri) {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, PIC_CROP);
} catch (ActivityNotFoundException e) {
String msg = "Your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
toast.show();
}
}
首先,Android 没有CROP
Intent
。
关于null
Uri
,应该是null
。引用ACTION_IMAGE_CAPTURE
文档:
调用方可能会传递额外的EXTRA_OUTPUT来控制此映像的写入位置。如果EXTRA_OUTPUT不存在,则在额外字段中将小尺寸图像作为位图对象返回。
你没有EXTRA_OUTPUT
,所以你应该通过记录不佳的(Bitmap)data.getExtras().get("data");
来获取你的图像。