Image Taken from Camera Not Saved to Gallery

Images not being saved when picture is taken by camera app that isn't the stock camera

It turns out my code was working after all. The pictures were being saved in the new directory, but the problem was that the gallery wasn't being updated, which explains why the photos would randomly appear in the directory later on. Being new to this, it never occurred to me that I would have to update the gallery. I only came to this realization after using ES File Explorer to look through my files. To fix my problem, I just made a new method in my CameraFragment that would call on the media scanner. I called this method from onActivityResult().

Here's the new method, though there's nothing really "new" about it since I ran into the same code on other SO questions:

protected void mediaScan() {
getActivity().sendBroadcast(
new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse(fileUri.toString())));
}

I also don't need to call the package manager and iterate through the apps that could handle the camera intent if I'm not giving the option to use choose a picture from a gallery, so I'm going to remove all that from my question.

Camera Intent doesn't save image to gallery

Your file exist in memory but not on disk. Create the file before returning the object

private static File getOutputMediaFile() { 
...
...
//create file on disk
if(!mediaFile.exists()) mediaFile.createNewFile();

return mediaFile;
}

Capture then Save the Image to Gallery not working Sometimes

Since you work with API 24 and higher, I will provide the code for it only. Basically, you need to tell the media scanner that a file was added so it can scan and add it straight away:

public static void scanMediaForChanges(Context context, File file){
MediaScannerConnection.scanFile(context,
new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
}

Internal App Camera Photos Improperly Saved to Gallery

I want these photos to stay 100% internal to my own app for privacy reasons

Then you should not be using ACTION_IMAGE_CAPTURE. By definition, that delegates the photo-taking to an arbitrary app, one of hundreds of possible apps based on device and user.

Instead, take the picture yourself, in your own app, either using the camera APIs directly or by using a library that wraps them.

On certain devices (not all of them) the photos taken within my app are being copied into the camera roll on the phone

Some camera apps will save the photos where they want, possibly in addition to copying the photo to your EXTRA_OUTPUT location. That is up to the developers of those camera apps, not you. Arguably, that behavior is a bug, but you have no practical means of getting any camera app developer to fix that bug, let alone developers of all buggy camera apps.



Related Topics



Leave a reply



Submit