简体   繁体   中英

I try scan media file in sdcard and refresh it

I use below code to get image file path and it thumbnail.

String[] projection = {MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA, MediaStore.Images.ImageColumns.DATA};  
Cursor cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.Media._ID); 
int count = cursor.getCount();
int image_column_index = cursor.getColumnIndex(MediaStore.Images.Media._ID);
int image_path_index = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
int i;
for(i = 0; i < count; i++) {
cursor.moveToPosition(i);
int id = cursor.getInt(image_column_index);
String p = cursor.getString(image_path_index);
Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(getApplicationContext().getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null);
}

And I try below code to refresh the database after file be modified.

Intent scanIntent = new Intent(Intent.ACTION_MEDIA_MOUNTED);
scanIntent.setData(Uri.fromFile(new File(Environment.getExternalStorageDirectory().getPath())));
sendBroadcast(scanIntent);

But while the file number more, it spend time more. So I want to confirm it finish to after work. How can I do to know it finish?

Run below:

IntentFilter intentfilter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_STARTED);
intentfilter.addDataScheme("file");
MediaScannerReceiver scanSdReceiver = new MediaScannerReceiver();
scanSdReceiver.setRestart(false);
egisterReceiver(scanSdReceiver, intentfilter);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath()))); 

And define class MediaScannerReceiver:

public class MediaScannerReceiver extends BroadcastReceiver {  
private boolean restart = false;
@Override  
public void onReceive(Context context, Intent intent) {  
String action = intent.getAction();
if(action.equals("android.intent.action.MEDIA_SCANNER_FINISHED")) {  
//update finish
} 
}  
public void setRestart(boolean r) {
this.restart = r;
}
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM