我已经看到了Android和/或硬件的某些版本不尊重MediaStore.EXTRA_OUTPUT标志。我有这样的评论+解决方案在我的代码:
/*
* HTC Eris and possibly other phones DONT respect the MediaStore.EXTRA_OUTPUT flag and they only
* return the image URI in the Data. In Android instances where the MediaStore.EXTRA_OUTPUT *IS* respected
* then Data is null. So use this fact retrieve the correct bitmap reference. When we have to return it from
* Data then just copy it to the fileUri that we tried to store it in the first place via
* MediaStore.EXTRA_OUTPUT.
*/
private void gotoCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String mBoothFileName = "snap_" + (new Date()).getTime() + ".jpg";
mStateHolder.mPictureFile = new File(((BatchApp) getApplication()).getStorageDirectory(), mBoothFileName);
mStateHolder.mPictureUri = Uri.fromFile(mStateHolder.mPictureFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mStateHolder.mPictureUri);
startActivityForResult(intent, TAKE_PICTURE);
}
/*
* When the post Camera activity returns
*/
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PICTURE) {
if (resultCode == RESULT_OK) {
/*
* HTC Eris and possibly other phones DONT respect the MediaStore.EXTRA_OUTPUT flag and they only
* return the image URI in the Data. In Android instances where the MediaStore.EXTRA_OUTPUT *IS* respected
* then Data is null. So use this fact retrieve the correct bitmap reference. When we have to return it from
* Data then just copy it to the fileUri that we tried to store it in the first place via
* MediaStore.EXTRA_OUTPUT. See how sneaky we are....
*/
if (data != null && data.getData() != null) {
Uri imageUri = data.getData();
try {
InputStream input = getContentResolver().openInputStream(imageUri);
FileOutputStream output = new FileOutputStream(mStateHolder.mPictureFile);
/*
From Apache Commons IO: copy one stream to another:
http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/IOUtils.html
*/
IOUtils.copy(input, output);
mStateHolder.mPictureUri = Uri.fromFile(mStateHolder.mPictureFile);
} catch (Exception e) {
Toast.makeText(this, "Oops - couldn't capture your picture.", Toast.LENGTH_LONG).show();
}
}
}
// you now have your image at "mStateHolder.mPictureUri"
// do whatever you need to do ...
}
}