Call MediaScanner in only one directory (Android)

I have a problem trying to start MediaScanner with only one directory.

My application takes snapshots and saves them on the SD card / DCIM / AppPictures /, and obviously I need to call MediaScanner to display them in the gallery application. The following code works for my purpose:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); 

But it just seems so wasteful! I do not want to use resources trying to scan the entire SD card when I know exactly where the files are. I also tried the following:

 sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory+"/DCIM/AppPictures/"))); 

But this does not give any results. Can someone PLEASE tell me the correct way to do this?

+6
source share
2 answers
 public static void ScanMyFile(String strFilePath) { // Tell the media scanner so it is available to the user. MediaScannerConnection.scanFile(null, new String[] { strFilePath }, null, new MediaScannerConnection.OnScanCompletedListener() { public void onScanCompleted(String path, Uri uri) { } }); } 
+1
source

In my application, I also upload photos.

When my main action (uploading images) begins, I initiate the connection of the media scanner. The download occurs sequentially, as soon as the image arrives, I save the file name in a line (with the name currentFile below) and β€œconnect” the media scanner:

 public class SomeActivity extends Activity implements MediaScannerConnectionClient { .... protected MediaScannerConnection mMs; .... @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); .... mMs = new MediaScannerConnection(this, this); .... } @Override public void onMediaScannerConnected() { mMs.scanFile(currentFile, null); } @Override public void onScanCompleted(String path, Uri uri) { mMs.disconnect(); } } 

To start a scan, I just do:

 currentFile = "/mnt/sdcard/someLocationToScan/somefile.jpg"; mMs.connect(); 
0
source

All Articles