views:

1657

answers:

5

I save an image to the sdcard and it doesn't appear in the Gallery application until I pull off the sdcard and return it back.

Do you have any idea why is it so?

Seems like the Gallery application has some cache that isn't updated on file save...

Actually, I also want to open the just-saved image in Gallery application and have no success with that
this is my question about this issue.

+3  A: 

The system scans the SD card when it is mounted to find any new image (and other) files. If you are programmatically adding a file, then you can use this class:

http://developer.android.com/reference/android/media/MediaScannerConnection.html

hackbod
Thank you for your answer. I will check your solution soon...
Michael Kessler
+2  A: 

Here is the code for the MediaScannerConnection:

MyMediaConnectorClient client = new MyMediaConnectorClient(newfile);
MediaScannerConnection scanner = new MediaScannerConnection(context, client);
client.setScanner(scanner);
scanner.connect();

newfile is the File object of your new/saved file.

3dmg
Thank you for your answer. Actually, I've already finished this project long time ago... I don't even remember what was the solution that I've used. Other users might find your solution helpful if you'd also provide the implementation of `MyMediaConnectorClient`...
Michael Kessler
yes - i'm having real headaches with this at the moment. Please post up :)
steve
do you need more infos?
3dmg
Yes I'm not really clear about how the flow should be -Correct me where I am wrongI can use MediaScannerConnection to update the SDCAARD info so I take picture with camera and pass the new file into the MSC and now I can access the file with with a connection scanner client?
steve
The flow is: you take a picture, save it to your sdcard and then make the scanner update, so that the filesystem gets updated to see your saved pic in the device gallery. If you dont do that with the scanner, you see your pic after the next device restart in the gallery.
3dmg
A: 

09-26 14:47:44.074: ERROR/MediaScannerService(10288): Failed to delete file /data/data/com.android.providers.media/pause_scan

I get this error when I try to call scanner.scanFile( szFinalFileName, null );

Any ideas?

dodobird
Please add as a comment to a relevant answer(s) and delete this answer before it will get downgraded. This way users that have supplied the answers will actually see your question (and not only me). Thank you.
Michael Kessler
A: 

My answer to the original question and to anyone else that may have this problem:

I was having this same problem, images in my app that people saved to the SD card were not showing up in their Gallery immediately. After some searching I found this one line of code inserted after my 'save to sdcard' code that fixed the problem:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
ShadowGod
Gotta love SO. If you're going to downvote this answer, that works perfectly by the way, at least explain why.
ShadowGod
A: 

You can also add an Image to the Media Gallery by intent, have a look at the example code to see how it is done:

ContentValues image = new ContentValues();

image.put(Images.Media.TITLE, imageTitle);
image.put(Images.Media.DISPLAY_NAME, imageDisplayName);
image.put(Images.Media.DESCRIPTION, imageDescription);
image.put(Images.Media.DATE_ADDED, dateTaken);
image.put(Images.Media.DATE_TAKEN, dateTaken);
image.put(Images.Media.DATE_MODIFIED, dateTaken);
image.put(Images.Media.MIME_TYPE, "image/png");
image.put(Images.Media.ORIENTATION, 0);

 File parent = imageFile.getParentFile();
 String path = parent.toString().toLowerCase();
 String name = parent.getName().toLowerCase();
 image.put(Images.ImageColumns.BUCKET_ID, path.hashCode());
 image.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, name);
 image.put(Images.Media.SIZE, imageFile.length());

 image.put(DATA_CONSTANT, imageFile.getAbsolutePath());

 Uri result = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, image);
Janusz