on api level 4 (android 1.6), after taking photo using:
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = new File(Environment.getExternalStorageDirectory(), "NewPic.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(intent, TAKE_PICTURE);
I'd like to look through all my photos thumbnails, but there is no my last photo thumbnail. It works perfectly on android 2.1.
If I connect device via USB to PC and then disconnect file will appear, after finished scanning. So how should I start that indexing?
I tried
mScanner = new MediaScannerConnection(getApplicationContext(), this);
mScanner.connect();
mScanner.scanFile(imageUri.getEncodedPath(), "*/*");
And end with this:
02-24 17:13:54.678: DEBUG/MediaScannerService(1320): IMediaScannerService.scanFile: /sdcard/NewPic2222.jpg mimeType: */*
02-24 17:13:54.688: VERBOSE/MediaProvider(1320): /sdcard volume ID: 1149784819
02-24 17:13:54.688: VERBOSE/MediaProvider(1320): key exists
EDITED LATER
I've got sth like this in other activity
mCursorThumbnails = MediaStore.Images.Thumbnails.queryMiniThumbnails(mContentResolver, MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, MediaStore.Images.Thumbnails.MINI_KIND, projection);
mCursorImages = MediaStore.Images.Media.query(mContentResolver, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection);
When I check count of first value I've got 13 elements, and on the second I've got 14. So the image has been added to mediascanner, but OS hasn't generated thumbnail for it. So how should I ask OS to create one?
after taking picture try calling insert() function of ContentResolver passing the information about the picture.
public final Uri insert (Uri url, ContentValues values)
It will actually add the picture to the database and create picture's thumbnail image for you. It will also be added to the thumbnail database. Hope this helps!
Whenever you add a file, let MediaStore Content Provider knows about it using the sendBroadcast method
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(mediaFileAdded)));
For deletion, use:
getContentResolver().delete(uriOfMediaFileDeteled, null, null)
Main advantage: work with any mime type supported by MediaStore
In your case, do this in onActivityResultMethod (i.e.) after the photo has been successfuly taken
Use this code:
public static void scanFile(Context context, String path, String mimeType ) {
Client client = new Client(path, mimeType);
MediaScannerConnection connection =
new MediaScannerConnection(context, client);
client.connection = connection;
connection.connect();
}
private static final class Client implements MediaScannerConnectionClient {
private final String path;
private final String mimeType;
MediaScannerConnection connection;
public Client(String path, String mimeType) {
this.path = path;
this.mimeType = mimeType;
}
@Override
public void onMediaScannerConnected() {
connection.scanFile(path, mimeType);
}
@Override
public void onScanCompleted(String path, Uri uri) {
connection.disconnect();
}
}
Then just call scanFile(imageUri.getPath(), null)
.
Don't use encoded path and don't use "*/*"
as a MIME type because null
value makes scanner to determine MIME type automatically.
Use
MediaStore.Images.Thumbnails.getThumbnail(ContentResolver cr, long origId, int kind, BitmapFactory.Options options)
to force the creation of the thumbnail for an image.
来源:https://stackoverflow.com/questions/5107823/force-scan-files-after-taking-photo