Android How to use MediaScannerConnection scanFile

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

    Let your activity implement 'MediaScannerConnectionClient' and add this to your activity:

    private void startScan() 
    { 
        if(conn!=null) conn.disconnect();  
        conn = new MediaScannerConnection(YourActivity.this,YourActivity.this); 
        conn.connect(); 
    } 
    
    @Override 
    public void onMediaScannerConnected() { 
        try{
            conn.scanFile(yourImagePath, "image/*");
           } catch (java.lang.IllegalStateException e){
           }
    }
    
    @Override 
    public void onScanCompleted(String path, Uri uri) { 
        conn.disconnect(); 
    } 
    

提交回复
热议问题