Android: Refreshing the Gallery after saving new images

后端 未结 8 906
天涯浪人
天涯浪人 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:56

    I tried Environment.getExternalStorageDirectory().toString(), but it didn't find my custom folder.

    I just wanted to share my experience to solving this issue.

    At the end, I had MediaScannerConnection configured to scan one file at a time and now missing folder that was holding those images showed up. Every time I download each image, I called MediaScannerConnection and file path as Uri instead of folder itself.

    Also, many people asked why sendBroadcast stop working on kitkat and the answer is that sendBroadcast was abused and called too many times and causing system to drain battery or slowdown, so they removed the direct call to prevent abuse which makes sense. I was hoping to use above solution to the folder that hold all images, but it didn't work on the folder. I was hoping that finding folder would expose rest of the files within the custom folder, but it didn't in my case. I am hoping to find better answer in the future...

    Here's snippet of my code.

    MediaScannerConnection.scanFile(ApplicationContext.context, new String[] { imageFile.getPath() }, null,
                  new MediaScannerConnection.OnScanCompletedListener() {
                    @Override
                    public void onScanCompleted(String path, Uri uri) {
                      Log.i(TAG, "Scanned " + path);
                    }
                  });
    
    0 讨论(0)
  • 2020-12-02 20:00

    Use MediaScannerConnection instead of SendBroadcast..

    MediaScannerConnection.MediaScannerConnectionClient(
    {
        @Override
        public void onScanCompleted(String path, Uri uri) {
    
            if (path.equals(**your filename**.getAbsolutePath()))
            {
                Log.i("Scan Status", "Completed");
                Log.i("uri: ",uri.toString());
    
                conn.disconnect();
            }
        }
        @Override
        public void onMediaScannerConnected()
        {
            // TODO Auto-generated method stub
            conn.scanFile(**your file name**.getAbsolutePath(),null);
        }
    });
    
    conn.connect();
    
    0 讨论(0)
提交回复
热议问题