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

后端 未结 5 1532
旧时难觅i
旧时难觅i 2020-12-01 06:47

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

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

相关标签:
5条回答
  • 2020-12-01 07:03

    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())));
            }
        }
    
    0 讨论(0)
  • 2020-12-01 07:05

    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.

    0 讨论(0)
  • 2020-12-01 07:17
    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);
            }
        });
    }
    
    0 讨论(0)
  • 2020-12-01 07:27

    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

    0 讨论(0)
  • 2020-12-01 07:28

    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");
    
    0 讨论(0)
提交回复
热议问题