Android: Refreshing the Gallery after saving new images

后端 未结 8 957
天涯浪人
天涯浪人 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: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();
    }
    

提交回复
热议问题