Method 1.
Old method, not recommended.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Force whole SDCard rescan.
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
Side-effect: Some devices will force close or cause System UI exception.
Method 2.
Send intent to scan specific file
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);You need a for loop or recursive call if your target is a directory.
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
Method 3.
Similar with method 2, but do your Media scan by yourself.
public class MediaScanner implements MediaScannerConnectionClient {Same as method 2, you need a for loop or recursive call if your target is a directory.
private MediaScannerConnection mScanner;
private File mFile;
public MediaScanner(Context context, File file) {
mFile = file;
mScanner = new MediaScannerConnection(context, this);
mScanner.connect();
}
@Override
public void onMediaScannerConnected() {
mScanner.scanFile(mFile.getAbsolutePath(), null);
}
@Override
public void onScanCompleted(String path, Uri uri) {
mScanner.disconnect();
}
}
public class YourClass {
...
new MediaScanner(context, file);
...
}
Suggest to use Thread or ExecutorService for a better performance.
No comments:
Post a Comment