Erik
Erik

Reputation: 5119

Android How to use MediaScannerConnection scanFile

Im adding images to a folder on the SDCARD. Since the images and my folder is not immediately visible in the Gallery im trying to get the MediaScannerConnection to update and show the folder/images in the gallery. This is not working so good for me since nothing shows up in Gallery. Im only testing in Eclipse AVD.

I dont see much talk about this maybe because the scanFile is new since api8. Could someone show how this is done?

Im trying it in both a service and Activity but keep getting uri=null when onScanCompleted.

Upvotes: 53

Views: 103414

Answers (8)

Petrus
Petrus

Reputation: 2616

I realized that perhaps you were looking for a solution what would work previous to api level 8, and I could not make sense of Mitch's answer. I solved it by building a class for scanning 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 would use it like this to make the MediaScannerConnection scan a single file:

new SingleMediaScanner(this, file);

Upvotes: 109

Pan
Pan

Reputation: 1

Use Intent instead of MediaScannerConnection. MediaScannerConnection will make your app 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);

Upvotes: 0

abhi5306
abhi5306

Reputation: 134

File file = new File(absolutePath);
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
sendBroadcast(intent);

Upvotes: 0

Kapil Jituri
Kapil Jituri

Reputation: 1261

Or you can use 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);
                    }
                });
    }

Call file scan as:

scanFile(yourFile.getAbsolutePath());

Upvotes: 30

Mitch
Mitch

Reputation: 481

Do not do the sendBroadcast if you only want one image to appear in the gallery. That'd be a huge waste of resources. You'll need to make a MediaScannerConnectionClient and then call connect() on the MediaScannerConnection you make from it to scan the file. Here's 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();

Upvotes: 11

TOMKA
TOMKA

Reputation: 1096

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

Upvotes: 5

Petrus
Petrus

Reputation: 2616

I was looking for the same thing and I found this in the ApiDemos, ExternalStorage. Solved all my problems as it scans 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);
      }
 });

Upvotes: 84

meizilp
meizilp

Reputation: 3921

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

Upvotes: 31

Related Questions