我尝试在SD卡中扫描媒体文件并刷新它

时间:2012-02-23 08:08:51

标签: android

我使用下面的代码来获取图像文件路径和缩略图。

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

我尝试使用以下代码在修改文件后刷新数据库。

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

但是当文件数量更多时,它会花费更多时间。 所以我想确认下班后完成。 我怎么知道它完成了?

1 个答案:

答案 0 :(得分:4)

运行如下:

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

定义类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;
}
}