【发布时间】:2019-01-21 15:46:57
【问题描述】:
我已经找到了一些据说可以让人们检索相机拍摄图像的 uri/位图的方法。 但是,当我尝试从非空 uri 中创建位图时,我得到的文件并未创建且不存在。
这是我用来打开相机意图的功能(从stackoverflow复制):
private void openBackCamera() {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = timeStamp + ".jpg";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
currentImageBase64 = storageDir.getAbsolutePath() + "/" + imageFileName;
File file = new File(currentImageBase64);
Log.d("2","file 1: " +file.exists());
Uri outputFileUri = FileProvider.getUriForFile(getApplicationContext(),"com.mydomain.fileprovider",file);
currentImageBase64 =outputFileUri+"";
Log.d("2",outputFileUri+" is the uri got from camera");
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
这就是我处理图像的方式:
Log.d("2","camera request!"+ currentImageBase64);
File imgFile = new File(currentImageBase64);
if(imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ivPreview.setImageBitmap(myBitmap);
}else{
Log.d("2","file does not exist");
}
请注意,相机请求日志显示 currentImagesBase64 不为空,并且具有值“content://com.mydomain.fileprovider/name/Pictures/20190121_173332.jpg” 无论如何,该文件不存在。
/** 编辑:问题是权限不足,请求了摄像头和读写权限,问题解决了! **/
【问题讨论】:
-
删除
currentImageBase64 =outputFileUri+"";。 -
@CommonsWare 不起作用 :(,相机请求日志“/storage/emulated/0/Pictures/20190121_181750.jpg”并且文件存在检查失败。
-
您是否拥有相应的权限,包括运行时权限?您是否使用 Android Studio 的设备文件资源管理器检查了该文件是否存在?
-
你为什么没有从你在 onActivityResult 方法中得到的 Intent 获取位图?
-
您是否在清单文件中添加了 FileProvider?
标签: android android-camera android-fileprovider