让Android识别新图像文件的方法有两种:(1)使用广播,(2)使用MediaScannerConnection的scanFile()。
// (1) using broadcast
public static void foobar1(Context context, File imageFile) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(imageFile);
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent);
}
// (2) using scanFile()
public static void foobar2(Context context, File imageFile) {
MediaScannerConnection.scanFile(
context, new String[] { imageFile.getPath() }, null,
new MediaScannerConnection.MediaScannerConnectionClient() {
@Override
public void onMediaScannerConnected() { }
@Override
public void onScanCompleted(String path, Uri uri) { }
});
}
哪种表现更好?