Force scan files after taking photo

杀马特。学长 韩版系。学妹 提交于 2019-11-27 14:14:47

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!