我试图从相机捕获图像后加载图像,但我得到“失败传递结果resultinfo”错误 . 以下是我收到错误时的值:
resultCode = -1
requestCode = 0
data = null
为什么Intent数据会变为null?这段代码有什么问题?
int REQUEST_CAMERA = 0
private void selectImage() {
final CharSequence[] items = { "Take Photo", "Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(ProfileSettings.this);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo"))
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg")));
startActivityForResult(intent, REQUEST_CAMERA);
}
else if (items[item].equals("Cancel"))
{
dialog.dismiss();
}
}
});
builder.show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CAMERA)
onCaptureImageResult(data);
}
}
private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mainImage.setImageBitmap(thumbnail);
}
1 回答
您正在提供
EXTRA_OUTPUT
. 如果您这样做,则不需要处理您的请求的相机应用程序通过Intent
返回结果 . 毕竟,您不需要结果,因为您知道写入完整大小图像的位置:到您通过EXTRA_OUTPUT
指示的位置 .