Android How to use MediaScannerConnection scanFile

I am adding images to a folder on sdcard. Since the images and my folder are not displayed immediately in the gallery im trying to update MediaScannerConnection and show the folder / images in the gallery. This does not work so well for me, since nothing appears in the gallery. Im just testing in Eclipse AVD.

I don't see much talk about this, possibly because scanFile is new with api8. Can anyone show how this is done?

I try to use it both in the service and in the Activity, but keep getting uri = null when onScanCompleted.

+33
android
Jan 10 '11 at 12:40
source share
9 answers
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); 
+27
Jan 10 '11 at 12:45
source share

I realized that maybe you were looking for a solution that would work up to level 8 of the api level, and I couldn't understand Mitch's answer. I solved this by building a class to scan a single file:

 import java.io.File; import android.content.Context; import android.media.MediaScannerConnection; import android.media.MediaScannerConnection.MediaScannerConnectionClient; import android.net.Uri; public class SingleMediaScanner implements MediaScannerConnectionClient { private MediaScannerConnection mMs; private File mFile; public SingleMediaScanner(Context context, File f) { mFile = f; mMs = new MediaScannerConnection(context, this); mMs.connect(); } @Override public void onMediaScannerConnected() { mMs.scanFile(mFile.getAbsolutePath(), null); } @Override public void onScanCompleted(String path, Uri uri) { mMs.disconnect(); } } 

and you should use it so that MediaScannerConnection scans a single file:

 new SingleMediaScanner(this, file); 

Hope this helps.

+86
Apr 28 '11 at 7:14
source share

I searched the same thing and found this in ApiDemos, ExternalStorage. Solved all my problems when it is viewing a single file.

  MediaScannerConnection.scanFile(this, 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); } }); 
+63
Apr 28 '11 at 6:29 a.m.
source share

Or you can use the following method:

 private void scanFile(String path) { MediaScannerConnection.scanFile(MainActivity.this, new String[] { path }, null, new MediaScannerConnection.OnScanCompletedListener() { public void onScanCompleted(String path, Uri uri) { Log.i("TAG", "Finished scanning " + path); } }); } 

Scan call files as:

 scanFile(yourFile.getAbsolutePath()); 
+16
Feb 19 '14 at 7:49
source share

Do not sendBroadcast if you want the gallery to display only one image. It will be a huge waste of resources. You will need to create a MediaScannerConnectionClient, and then call connect () in the MediaScannerConnection, which you create to scan the file. Here is an example:

 private MediaScannerConnectionClient mediaScannerConnectionClient = new MediaScannerConnectionClient() { @Override public void onMediaScannerConnected() { mediaScannerConnection.scanFile("pathToImage/someImage.jpg", null); } @Override public void onScanCompleted(String path, Uri uri) { if(path.equals("pathToImage/someImage.jpg")) mediaScannerConnection.disconnect(); } }; new MediaScannerConnection(context, mediaScannerConnectionClient).connect(); 
+10
Jan 28 2018-11-11T00:
source share

Let your activity implement "MediaScannerConnectionClient" and add this to your activity:

 private void startScan() { if(conn!=null) conn.disconnect(); conn = new MediaScannerConnection(YourActivity.this,YourActivity.this); conn.connect(); } @Override public void onMediaScannerConnected() { try{ conn.scanFile(yourImagePath, "image/*"); } catch (java.lang.IllegalStateException e){ } } @Override public void onScanCompleted(String path, Uri uri) { conn.disconnect(); } 
+4
Feb 01 2018-12-12T00:
source share

I wrote a detailed answer here, Trigger media scanner on a specific path (folder), how?

I hope it solves your problem perfectly.

+1
Aug 01 '14 at 18:37
source share
 File file = new File(absolutePath); Uri uri = Uri.fromFile(file); Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri); sendBroadcast(intent); 
0
Mar 19 '14 at 6:30 a.m.
source share

Use Intent instead of MediaScannerConnection . MediaScannerConnection will make your application gc error with IMediaScannerListener.Stub mListener .

 Intent mediaScannerIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri fileContentUri = Uri.parseFile(permFile); // With 'permFile' being the File object mediaScannerIntent.setData(fileContentUri); sendBroadcast(mediaScannerIntent); 
0
Dec 01 '17 at 3:27
source share



All Articles