Image not show in gallery - android

I launch default camera using intent and store those camera images in external storage using below path:
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + fileName);
But it does not show in gallery. The issue comes in nexus 4,7 and moto G devices with OS 4.4.2
I try with
mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri
.parse("file://" + Environment.getExternalStorageDirectory())));
But It not work

You will have to refresh the media scanner cache, try this:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(yourFile)));

You have to tell the device to scan the new media file. Try with this snippet:
MediaScannerConnection.scanFile(
this, new String[]{file.getAbsolutePath()}, null, null);

I think you'd better make a folder in the camera default folder and put your data in it. Check if a file with the name of ".nomedia" is not in that folder.

Related

How to call system's media scanner?

My app downloads .mp3 files to download folder. It works fine, but then no music player app is able to play it ("cannot play this type of files").
I have to reboot every time to force the system to run media scanner, to make it work.
I tried MediaScannerConnection, but it does not work on my API level (24)
File sdCard = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
MediaScannerConnection.scanFile(this, new String[]{sdCard.getPath()},
null, new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
// now visible in gallery
Toast.makeText(getBaseContext(), "files have been scanned dawg", Toast.LENGTH_LONG).show();
}
});
It does not show any toast.
I have found another method, but I have heard that it breaks from android 4.4
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
This method breaks from android 7 apparently
File file = new File(absolutePath);
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
sendBroadcast(intent);
How do I make it work on api level 15-current? Is there any universal way? None of them worked on my device yet.

Android - Generate Thumbnails in Gallery

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())));

Delete file on Android SD card

In my application I can take a photo and save it with this code:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageCaptureUri = Uri.fromFile(new File(context.getExternalFilesDir(null),
"tmp_image_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageCaptureUri);
intent.putExtra("return-data", true);
I tried to delete the file from my SD card with this code:
File f = new File(imageCaptureUri.getPath());
if (f.exists() == true)
{
boolean state = f.delete();
Toast.makeText(context, "" + state, Toast.LENGTH_LONG).show();
}
The output of the Toast is true but the file is not deleted on the SD card. I tested this on Android KitKat but it should work on older android versions too. Do you have any ideas why I can't delete the photo?
I used android.permission.WRITE_EXTERNAL_STORAGE in my manifest file.
With some quick searching, i see that occasionally a call to System.gc() is occasionally necessary. Call System.gc() after your f.delete()
KitKat have some changes on how apps can or not access files. Even in the external storage.
Because that's a temp file, I suggest you to relocate it to your own app space like context.getCacheDir instead. That will probably work.

Android ACTION_MEDIA_SCANNER_SCAN_FILE not showing image until reboot?

I am currently developing an Android Application where users take pictures which are stored in External Memory on the device then I am trying to also get the Gallery to scan the file to add the new media to the Gallery however this does not seem to update until the device reboots.
The code I am using is as follows:
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Log.v("INFO", mCurrentPhotoPath.toString());
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
After taking a picture with my app I can verify it exists using a file manager app on the device and also Logging the file path it appears to be correct before passing it to Media Scanner
file:/storage/emulated/0/Pictures/JPEG_20140712_163043_1418054327.jpg
Thanks Aaron
I managed to solve this. The issue was due to, where I setup the mCurrentPhotoPath. So I updated the code to use
photoFile = createImageFile();
mCurrentPhotoPath = photoFile.getAbsolutePath();
Media Scanner take some to scan and insert show results after calling sendBroadcast you can use MediaScannerConnection method onScanCompleted
and then check image in gallery.
for more reference follow Link

Thumbnail isn't refresh immediately

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.

Categories

Resources