是否可以使用MediaScanner递归扫描文件夹

时间:2013-08-16 10:11:20

标签: android android-mediascanner fileobserver

我有一个关于MediaScanner的基本问题请帮助我更好地理解这一点。

文件夹的MediaScanner仅返回有关扫描的文件夹的信息,但不返回其中存在的任何媒体文件的文件夹内容。如果扫描文件夹是为ex。其中包含少量.jpg文件的/storage/extSdCard/DCIM/Camera,我得到有关该文件夹的扫描响应,而不是其中的有关文件。记录下面,

D/MediaScannerConnection(29105): Connected to MediaScanner
D/MediaScannerConnection(29105): Scanning file 7klwibgf7fxlKdCbid7DCRV7Cbvfib

D/MyLog(29105): onScanCompleted(path,uri), path: /storage/extSdCard/DCIM/Camera, Uri: content://media/external/file/4334

我的问题, 是否有可能在将媒体文件添加到我感兴趣的文件夹时将事件恢复,在这种情况下,/storage/extSdCard/DCIM/Camera

    public void MyScanner(String filename){
        File fileCheck = new File(filename);
        conn = new MediaScannerConnection(this,this);
        conn.connect();
}

@Override
    public void onMediaScannerConnected() {
        Log.d(TAG,"onMediaScannerConnected() to MediaScanner Service");
        conn.scanFile(filename, null);
    }

@Override
    public void onScanCompleted(String path, Uri uri) {
        if(path!=null && uri!=null)
        Log.d(TAG,"onScanCompleted(), path: "+path+", Uri: "+uri.toString());
        Log.d(TAG,"Converting file uri to FilePath: "+getRealPathFromURI(uri)); 
        }

1 个答案:

答案 0 :(得分:0)

  First One Code....

 scanPhoto(imageFileName.toString());



public void scanPhoto(final String imageFileName)
{
  msConn = new MediaScannerConnection(PreviewDemo1.this,new MediaScannerConnectionClient()
  {
                public void onMediaScannerConnected()
                {
                    msConn.scanFile(imageFileName, null);
                    Log.i("msClient obj  in Photo Utility","connection established");
                }
                public void onScanCompleted(String path, Uri uri)
                {
                    msConn.disconnect();
                    Log.i("msClient obj in Photo Utility","scan completed");
                }
    });
    msConn.connect();
 } 

第二个代码..

 sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));