【发布时间】:2017-08-04 06:51:41
【问题描述】:
我正在尝试使用代码打开相机拍照 它的工作正常到 android 6.0 。 但是在 android 7.0 版本中它给出了错误
错误
file:///storage/emulated/0/04082017_1136image.jpg 通过 ClipData.Item.getUri() 暴露在应用之外
private void takePhotoFromCamera()
{
AnimateImageButton();
boolean result = Utility.checkPermission(MainActivity.this);
if (result) {
try {
_isOpenGallery = false;
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
File file = new File(Environment.getExternalStorageDirectory() + File.separator + timeStamp + "image.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, REQUEST_CAMERA);
System.out.println("Hello >>>>>>>> : " + file.getAbsolutePath());
}catch (Exception e)
{
Log.d("logforcamera",e.getMessage());
}
}
}
活动结果代码
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
File file = new File(Environment.getExternalStorageDirectory() + File.separator + timeStamp + "image.jpg");
System.out.println("Helllloooo >>>>>>>>>>> : " + file.getAbsolutePath());
Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), widthX, heightY);
System.out.println("Bitmap : " + bitmap);
if (bitmap != null)
{
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
showSelectedImage(bitmap);
}
【问题讨论】:
-
放上ActivityResult代码
-
它给出错误。 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
-
它只在 android 7.0 中给出错误
-
在其他安卓版本中工作正常
标签: android camera android-7.0-nougat