Android How to use MediaScannerConnection scanFile

前端 未结 8 1660
醉梦人生
醉梦人生 2020-11-22 07:20

Im adding images to a folder on the SDCARD. Since the images and my folder is not immediately visible in the Gallery im trying to get the MediaScannerConnection to update an

8条回答
  •  一向
    一向 (楼主)
    2020-11-22 07:50

    Do not do the sendBroadcast if you only want one image to appear in the gallery. That'd be a huge waste of resources. You'll need to make a MediaScannerConnectionClient and then call connect() on the MediaScannerConnection you make from it to scan the file. Here's an example:

    private MediaScannerConnectionClient mediaScannerConnectionClient = 
        new MediaScannerConnectionClient() {
    
        @Override
        public void onMediaScannerConnected() {
            mediaScannerConnection.scanFile("pathToImage/someImage.jpg", null);
        }
    
        @Override
        public void onScanCompleted(String path, Uri uri) {
            if(path.equals("pathToImage/someImage.jpg"))
                mediaScannerConnection.disconnect();
        }
    };
    new MediaScannerConnection(context, mediaScannerConnectionClient).connect();
    

提交回复
热议问题