最近项目中遇到一个问题,Android手机拍照返回时,部分手机拍照返回时,在onActivityResult方法中使用data.getData()返回为空,解决方法为:
private Uri photoUri=null;
/**
* 调用系统相机拍照
* */
private void openFile() {
Intent openFileIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
openFileIntent
.putExtra(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
SimpleDateFormat timeStampFormat = new SimpleDateFormat(
"yyyy_MM_dd_HH_mm_ss");
String filename = timeStampFormat.format(new Date());
ContentValues values = new ContentValues();
values.put(Media.TITLE, filename);
photoUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
openFileIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(openFileIntent, Consts.Intents.INTENT_OPEN_FILE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK
&& requestCode == Consts.Intents.INTENT_OPEN_FILE) {
Uri result = null;
if (ValueUtils.isNotEmpty(data)) {
result = data.getData();
}else {
result = photoUri;
}
if (mUploadMessage != null) {
if (result != null) {
mUploadMessage.onReceiveValue(result);
} else {
// 防止监听一直挂起
mUploadMessage.onReceiveValue(null);
}
mUploadMessage = null;
}
}
}