Android: Refreshing the Gallery after saving new images

后端 未结 8 905
天涯浪人
天涯浪人 2020-12-02 19:20

So in my application I at one point save a bunch of images to a temporary folder, and I want them to show up immediately in the Gallery. Off of a reboot, they do, but otherw

相关标签:
8条回答
  • 2020-12-02 19:38

    Code provided by Petrus in another answer works for me on Kitkat (4.4):

    MediaScannerConnection.scanFile(this, new String[] { Environment.getExternalStorageDirectory().toString() }, null, new MediaScannerConnection.OnScanCompletedListener() {
    /*
     *   (non-Javadoc)
     * @see android.media.MediaScannerConnection.OnScanCompletedListener#onScanCompleted(java.lang.String, android.net.Uri)
     */
    public void onScanCompleted(String path, Uri uri) 
      {
          Log.i("ExternalStorage", "Scanned " + path + ":");
          Log.i("ExternalStorage", "-> uri=" + uri);
      }
        });
    
    0 讨论(0)
  • 2020-12-02 19:43

    Try this one: after saving your file to folder,write below code

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File("your image path"))));
    
    0 讨论(0)
  • 2020-12-02 19:43
     FileOutputStream out = new FileOutputStream(file);
                finalBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
                out.flush();
    //Gallery Refresh Code         
    
        MediaScannerConnection.scanFile(getActivity(), new String[] { Environment.getExternalStorageDirectory().toString() }, null, new MediaScannerConnection.OnScanCompletedListener() {
                    /*
                     *   (non-Javadoc)
                     * @see android.media.MediaScannerConnection.OnScanCompletedListener#onScanCompleted(java.lang.String, android.net.Uri)
                     */
                    public void onScanCompleted(String path, Uri uri)
                    {
                        Log.i("ExternalStorage", "Scanned " + path + ":");
                        Log.i("ExternalStorage", "-> uri=" + uri);
                    }
                });
                out.close();
    
    0 讨论(0)
  • 2020-12-02 19:53

    Implement MediaScannerConnectionClient and add the below code.Works perfectly in 4.4 :)

    MediaScannerConnection conn;
    
    public void startScan(String url) {
        imagepath = url;
        if (conn != null)
            conn.disconnect();
        conn = new MediaScannerConnection(activity.this, activity.this);
        conn.connect();
    }
    
    @Override
    public void onMediaScannerConnected() {
        try {
            conn.scanFile(imagepath, getMimeType(imagepath));
        } catch (java.lang.IllegalStateException e) {
            //Do something
        }
    }
    
    @Override
    public void onScanCompleted(String path, Uri uri) {
        conn.disconnect();
    }
    
    0 讨论(0)
  • 2020-12-02 19:54

    Below code work all device for refreshing the gallery after saving image.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
           Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
           File f1 = new File("file://" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES));
           Uri contentUri = Uri.fromFile(f1);
           mediaScanIntent.setData(contentUri);
           sendBroadcast(mediaScanIntent);
       } else {
           sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
       }
       sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File("your image path"))));
    
    0 讨论(0)
  • 2020-12-02 19:55

    I know its late but try this can working in all versions:

       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                        Intent mediaScanIntent = new Intent(
                                Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                        Uri contentUri = Uri.fromFile(out); //out is your file you saved/deleted/moved/copied
                        mediaScanIntent.setData(contentUri);
                        this.sendBroadcast(mediaScanIntent);
                    } else {
                        sendBroadcast(new Intent(
                                Intent.ACTION_MEDIA_MOUNTED,
                                Uri.parse("file://"
                                        + Environment.getExternalStorageDirectory())));
                    }
    
    0 讨论(0)
提交回复
热议问题