I am currently creating a camera app and storing it. However, every time I take a picture, the picture is saved in the directory but is not saved in the Gallery. I think it's all about the generation of thumbnail right after the image has been taken. How can you generate a thumbnail in the gallery?
Try this:
Intent mediaScan = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScan.setData(uri);
context.sendBroadcast(mediaScan);
If you are adding files or images programmatically, you have to use MediaScannerClass to scan the media file for your sd card.
of you can also use following piece of code:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
Related
I have a small app that calls the intent of image_capture from android, takes and then deletes it from the gallery.
Well, the picture is not deleted, well, not completely, the best result that I get it's this:
My code is simple, based on what I found around internet:
success = new File(imageUrl).delete();
//deleteFile(imageUrl);
if(success)
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
What can I do to solve this problem?
I have a Bitmap saved in the directory: Environment.getExternalStorageDirectory()+ File.separator + "Images" in a subdirectory. With a fileManager i can find the image and open it. But in the Gallery of the Smartphone i can't find the Image.
How can i get it there?
To be make you file known by the Gallery you need to call the MediaScanner:
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
See also: https://www.grokkingandroid.com/adding-files-to-androids-media-library-using-the-mediascanner/
You can actually directly access the Pictures directory. To do this, use
getExternalStoragePublicDirectory(String type)
, where type is equal to Environment.DIRECTORY_PICTURES.
See http://developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory%28java.lang.String%29 for more info.
That's cleaner than supposing that pictures are always stored in a directory called "Images"...
Now to force the Gallery to register the change, look at https://stackoverflow.com/a/15837638/2984973 .
To quote the answer, you want to broadcast an intent specifying your new picture, likewise:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(myNewFile)));
I am using list view and gallery in android.. i have 3 views as list items i.e a contact photo,name and a button to add that contact . when user clicks the button inside list view,the contact photo of that items should add in gallery... Can anybody please tell how to achieve this?
#Nidhi can you add image like that
private void addPicToGallery() {
Intent media = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
//mCurrentPhotoPath is the path to image.
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
media.setData(contentUri);
this.sendBroadcast(media);
}
save image in sdcard. But after this, you will not be able to see that image in gallery. To see this image, You will have to refresh sdcard using
broadcast(sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath())));)
when you will save this image.
If my understanding is correct, Why can't you just download the image and save them in sd card? It will show up in gallery automatically. if it is not showing in gallery you can call the following to call implicitly after downloading the images to sd card.
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
I'm making a file manager in which picture items have a small thumbnail.
I get thumbnail image by using MediaStore. Everything works fine. But when I rename or move a file, the thumbnail does not show up.
I've found a piece of code to refresh MediaStore:
getActivity().sendBroadcast(
new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
It worked but I must wait 4 or 5 second and refresh, then the thumbnail updates.
How to get thumbnail of image immediately after rename or moving?
What happen if you use ACTION_MEDIA_SCANNER_SCAN_FILE instead of ACTION_MEDIA_MOUNTED, (i.e. trigger a refresh for a single file instead of for the complete directory hierarchy) ?
You will need to replace the URI of the directory with the URI of the file, obtained for example using Uri.fromFile().
When you move or rename a file you should refresh the old and the new URIs.
The recommended way to update one specific image in Android is using ACTION_MEDIA_SCANNER_SCAN_FILE intent. And for smoother
You can check it at Basic Photo Handling Training in Android Developer Site.
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);
this.sendBroadcast(mediaScanIntent);
}
If you want to show new thumbnail immediately for some missing files, you can do it by yourself. First, check the MediaStore as before, and if the returned thumbnail is null then generate your own one using ThumbnailUtils or BitmapFactory.
And, For handling a bitmap and displaying it, there is a quiet straightforward sample in Android Training Course.
Have you tried doing the scan directly on the directory you are changing? So instead of
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
something like
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory() + "/path/to/your/folder")));
An alternative would be to manually use ThumbnailUtils.
Actually sending Intent.ACTION_MEDIA_MOUNTED broadcast intent is really ugly. Read this post http://androidyue.github.io/blog/2014/01/19/scan-media-files-in-android As to renaming files. You should remove the older file from the library and then add the new one into the library. I think this could help you.
I'm trying to save the image captured via android camera in a custom location. My code looks like this:
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File f = File(android.os.Environment.getExternalStorageDirectory(), "test.jpg");
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, SUB_ACTIVITY_CAMERA_PREVIEW);
The image is being saved to the desired location but it is also being saved to the default camera folder which is causing it to be displayed in the gallery. Any suggestions?
Include an empty file named .nomedia in your external files directory (note the dot prefix in the filename). This will prevent Android's media scanner from reading your media files and including them in apps like Gallery or Music.