Android media scan not running second time
-
12-06-2021 - |
Question
I am working on a code that could scan the specific folder, when picture is taken, I used the following code:
Intent intent = new Intent();
intent.setType("image/*");
//intent.setAction(Intent.ACTION_GET_CONTENT);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://sdcard/ghost/" + Environment.getExternalStorageDirectory())));
startActivityForResult(
Intent.createChooser(intent, "Select Picture"), 0);
It works fine, but when I exit the app and start the app again, scan is not performed, what could be the issue?
Help needed!!!
Solution
instead of using sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://sdcard/ghost/" + Environment.getExternalStorageDirectory())));
i used
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
OTHER TIPS
This is what I use to have it scan a specific file.
Set file path
String sdPicPath = Environment.getExternalStorageDirectory().getAbsoluteFile() + "/image.png";
or if I have an albumn
String sdPicPath = Environment.getExternalStorageDirectory().getAbsoluteFile() + "AlbumName/image.png";
Create a File
File CheckForAlbum = new File(sdAlbumPath);
Have MediaScan scan the specific file.
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.fromFile(CheckForFile1)));