简体   繁体   中英

How can I refresh the Gallery after I inserted an Image in android?

I've added an inserted in Gallery using android API as following:

Images.Media.insertImage(ctx.getContentResolver(), "scard/test.jpg", "Hello" , "description");

Actually the image that I passed its full path (scard/test.jpg) is already successfully inserted in the DB, but when you open the gallery you can't see it unless you switch off/on the device or Mount/Unmount the external memory.

It there any way to refresh the gallery on demand?

Thanks

Bassel Kh.

I encountered this problem recently, I tried @Samuh's solution, but not works perfectly until I found the solution from Google's example :

   // Tell the media scanner about the new file so that it is
    // immediately available to the user.
    MediaScannerConnection.scanFile(this,
            new String[] { file.toString() }, null,
            new MediaScannerConnection.OnScanCompletedListener() {
        public void onScanCompleted(String path, Uri uri) {
            Log.i("ExternalStorage", "Scanned " + path + ":");
            Log.i("ExternalStorage", "-> uri=" + uri);
        }
    });

I tried myself and worked flawlessly.

Or, similarly, you might want to take a look at the reference of the MediaScanner Class and someone on StackOverflow asked this question before: Image, saved to sdcard, doesn't appear in Android's Gallery app

The solution using the Media Scanner (sendBroadcast). But, probably, on sdcards with a lot of pics and data, this operation should reach a high processing cost.

There is another solution, after saving your media file on gallery, you should notify the gallery DB that another file was inserted. That can be done like that:

private void addImageGallery( File file ) {
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); // or image/png
    getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}

This code doesn't save your media file to gallery, only saves the information about a new file in the gallery DB. You should store real file before.

This solution is faster because the gallery will not be fully re-scanned. But is not so trustful because all the information about the file was added manually.

static public boolean resetExternalStorageMedia(Context context) {
    if (Environment.isExternalStorageEmulated())
        return (false);
    Uri uri = Uri.parse("file://" + Environment.getExternalStorageDirectory());
    Intent intent = new Intent(Intent.ACTION_MEDIA_MOUNTED, uri);

    context.sendBroadcast(intent);
    return (true);
}

static public void notifyMediaScannerService(Context context, String path) {
    MediaScannerConnection.scanFile(context,
            new String[] { path }, null,
            new MediaScannerConnection.OnScanCompletedListener() {
        public void onScanCompleted(String path, Uri uri) {
            Log.i("ExternalStorage", "Scanned " + path + ":");
            Log.i("ExternalStorage", "-> uri=" + uri);
        }
    });
}

You can use this for refresh Android Gallery:

public void refreshAndroidGallery(Uri fileUri) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Intent mediaScanIntent = new Intent(
                    Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            mediaScanIntent.setData(fileUri);
            mContext.sendBroadcast(mediaScanIntent);
        } else {
            mContext.sendBroadcast(new Intent(
                    Intent.ACTION_MEDIA_MOUNTED,
                    Uri.parse("file://" + Environment.getExternalStorageDirectory())));
        }
    }

i tied everything then I found this perfect solution! You can apply this method for any file type (jpg, png,pdf, etc)

public void refreshGallery(File f) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Intent mediaScanIntent = new Intent(
                Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri fileUri = Uri.fromFile(f); //out is your output file
        mediaScanIntent.setData(fileUri);
        sendBroadcast(mediaScanIntent);
    } else {
        sendBroadcast(new Intent(
                Intent.ACTION_MEDIA_MOUNTED,
                Uri.parse("file://" + Environment.getExternalStorageDirectory())));
    }
}
    

then

refreshGallery(newFileName);

newFIleName is a file path you can get your file path as

File newFileName = new File(filePath + "/" + fileName + ".jpg");

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