I am trying to make an app in which the user can take a picture to attach to a post. For the image capturing, saving and handling part I was following the official developer guide (using their code snippets). I have no problem with taking the picture and saving it to external storage (I have found the pictures on my hard drive), but when I need to access the file for displaying the image in the view (or for starting a media scanner intent), I get an error indicating that the file was not found.
Here are some parts of my code:
File creation:
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;
}
Image capture intent:
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);
}
}
}
Displaying small image:
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);
}
Adding image to gallery:
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);
}
And then when I run the app, after taking the picture I get this in 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)
I set the permission to write to external storage in the manifest and I can see the pictures saved to my storage, so the file exists. I was really just doing everything according to the official guide, so I have no idea what is going wrong. Can someone help me? Thanks in advance!
EDIT:
I have changed the line
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
to
mCurrentPhotoPath = image.getAbsolutePath();
so the createImageFile() function now looks like this:
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;
}
This solved my problem with displaying the image, but the photo still does not appear in my gallery. When I log the value of the URI passed to the media scan intent, I get this:
file:///storage/emulated/0/Pictures/JPEG_20150731_135706_153874160.jpg
Could this be the problem (although I get it from the image file with Uri.fromFile method, so this should be a proper URI)? Or what else could cause the problem?
Change this line
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
to
mCurrentPhotoPath = image.getAbsolutePath();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.