问题描述
我正在尝试制作一个应用程序,用户可以在其中拍照以附加到帖子。 对于图像捕获,保存和处理,我遵循官方开发人员指南(使用其代码段)。 我拍摄照片并将其保存到外部存储没有问题(我已经在硬盘上找到了照片),但是当我需要访问文件以在视图中显示图像(或启动媒体扫描仪意图)时,我没有问题,我收到一条错误消息,指出未找到该文件。
这是我的代码的某些部分:
文件创建:
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
图像捕获意图:
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Toast.makeText(getActivity(), ex.getMessage(), Toast.LENGTH_SHORT).show();
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
显示小图像:
private void setPic() {
// Get the dimensions of the View
int targetW = 30;
int targetH = 50;
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
img_added.setImageBitmap(bitmap);
}
将图片添加到图库:
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
getActivity().sendBroadcast(mediaScanIntent);
}
然后,当我运行应用程序时,在拍照后,我在logcat中得到了这个:
E/BitmapFactory﹕ Unable to decode stream: java.io.FileNotFoundException: file:/storage/emulated/0/Pictures/JPEG_20150731_114600_153874160.jpg: open failed: ENOENT (No such file or directory)
我在清单中设置了写入外部存储的权限,并且可以看到保存到我的存储中的图片,因此该文件存在。 根据官方指南,我实际上只是在做所有事情,所以我不知道出了什么问题。 有人能帮我吗? 提前致谢!
编辑:
我换了线
mCurrentPhotoPath =“ file:” + image.getAbsolutePath();
至
mCurrentPhotoPath = image.getAbsolutePath();
所以createImageFile()函数现在看起来像这样:
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
这解决了我显示图像的问题,但是照片仍然没有出现在我的画廊中。 当我记录传递给媒体扫描意图的URI的值时,得到以下信息:
文件:///storage/emulated/0/Pictures/JPEG_20150731_135706_153874160.jpg
可能是问题所在(尽管我是使用Uri.fromFile方法从图像文件中获得的,所以这应该是正确的URI)? 或者还有什么可能导致问题?
1楼
更改此行
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
至
mCurrentPhotoPath = image.getAbsolutePath();