昨天调用系统相机拍照制作头像的时候遇到个问题,相机拍完照后返回后直接闪退,那么问题出在哪呢?先看看当时的代码:
Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,getImageUri());
cameraIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
activity.startActivityForResult(cameraIntent,Constants.CAMERA_REQUEST_CODE);
//onActivityResult回调中,
if (resultCode != RESULT_OK) {resizeImage是裁剪图片
return;
} else {
switch (requestCode) {
case Constants.CAMERA_REQUEST_CODE:
if (FileUtil.isSdCardExist()) {
SelectPhotoUtil.resizeImage(MyPortraitActivity.this, data.getData());
} else {
Toast.makeText(this, "未找到存储卡", Toast.LENGTH_SHORT).show();
}
break;
}
直接报data为空!!!
这是为什么呢,看一下源码
// First handle the no crop case -- just return the value. If the
// caller specifies a "save uri" then write the data to it's
// stream. Otherwise, pass back a scaled down version of the bitmap
// directly in the extras.
if (mSaveUri != null) { //存在mSaveUri,即指定了目标uri
OutputStream outputStream = null;
try {
outputStream = mContentResolver.openOutputStream(mSaveUri);
outputStream.write(data);
outputStream.close();
setResult(RESULT_OK); //直接返回RESULT_OK,并没有指定intent
finish();
} catch (IOException ex) {
// ignore exception
} finally {
Util.closeSilently(outputStream);
}
} else {
Bitmap bitmap = createCaptureBitmap(data);
// 返回RESULT_OK,并包含一个Intent对象,其中Extra中科key为data,value为一个bitmap
setResult(RESULT_OK, new Intent("inline-data").putExtra("data", bitmap));
finish();
}
也就是说如果指定MediaStore.EXTRA_OUTPUT的uri值返回的data值就是空的,无语。。。。
有什么解决办法呢?