android相机保存文件为空,相机不保存到指定的文件位置android

我已经看到了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 ...

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值