Android How to use MediaScannerConnection scanFile

前端 未结 8 1659
醉梦人生
醉梦人生 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:56

    Or you can use following method:

    private void scanFile(String path) {
    
            MediaScannerConnection.scanFile(MainActivity.this,
                    new String[] { path }, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
    
                        public void onScanCompleted(String path, Uri uri) {
                            Log.i("TAG", "Finished scanning " + path);
                        }
                    });
        }
    

    Call file scan as:

    scanFile(yourFile.getAbsolutePath());
    
    0 讨论(0)
  • 2020-11-22 07:59

    Use Intent instead of MediaScannerConnection. MediaScannerConnection will make your app gc error with IMediaScannerListener.Stub mListener.

    Intent mediaScannerIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    Uri fileContentUri = Uri.parseFile(permFile); // With 'permFile' being the File object
    mediaScannerIntent.setData(fileContentUri);
    sendBroadcast(mediaScannerIntent);
    
    0 讨论(0)
提交回复
热议问题