在特定路径(文件夹)上触发mediascanner,如何?

我上了这个课:


import android.content.Context;

import android.media.MediaScannerConnection;

import android.net.Uri;

import android.util.Log;


public class MediaScannerWrapper implements  

MediaScannerConnection.MediaScannerConnectionClient {

    private MediaScannerConnection mConnection;

    private String mPath;

    private String mMimeType;



    // filePath - where to scan; 

    // mime type of media to scan i.e. "image/jpeg". 

    // use "*/*" for any media

    public MediaScannerWrapper(Context ctx, String filePath, String mime){

        mPath = "/sdcard/DCIM/Camera";

        mMimeType = "jpg";

        mConnection = new MediaScannerConnection(ctx, this);

    }


    // do the scanning

    public void scan() {

        mConnection.connect();

    }


    // start the scan when scanner is ready

    public void onMediaScannerConnected() {

        mConnection.scanFile(mPath, mMimeType);

        Log.w("MediaScannerWrapper", "media file scanned: " + mPath);

    }


    public void onScanCompleted(String path, Uri uri) {

        // when scan is completes, update media file tags

    }

}

如何在其他课程中使用它?我不知道如何正确使用类,我尝试了但没有任何效果。我做错了事,但我不知道该怎么办,有人可以帮我这个忙。


qq_花开花谢_0
浏览 754回答 3
3回答

德玛西亚99

故事在Android 4.4之前的版本中,我们可以发送广播以触发任何特定文件,文件夹甚至存储根目录上的媒体扫描器。但是从4.4 KitKat开始,此问题已由Android开发人员修复。为什么我说固定的?原因很简单。在根目录上使用MEDIA_MOUNTED发送广播非常昂贵。运行Media Scanner是一项昂贵的操作,并且当用户在存储和深层文件夹结构中拥有大量文件时,情况甚至更糟。在Android 4.4之前保持简洁明了。如果您要在Android 4.4之前定位您的应用。但是请记住,除非绝对必要,否则不要在根目录上使用它。sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));从Android 4.4有两种方式适合您。i)第一个与前面的示例非常相似,但是可能无法有效工作,也不建议这样做。sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + Environment.getExternalStorageDirectory())));ii)现在,让我们继续针对该问题的最推荐和最有效的解决方案。像这样,在String类型ArrayList中添加已更新文件的文件路径。ArrayList<String> toBeScanned = new ArrayList<String>();toBeScanned.add(item.getFilePath());现在,您需要运行MediaScannerConnection类的scanFile()静态方法,并传递String数组,其中包含已更新并需要进行媒体扫描的所有文件的列表。您也可以让侦听器在完成对单个文件的扫描后做出响应。String[] toBeScannedStr = new String[toBeScanned.size()];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toBeScannedStr = toBeScanned.toArray(toBeScannedStr);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MediaScannerConnection.scanFile(getActivity(), toBeScannedStr, null, new OnScanCompletedListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onScanCompleted(String path, Uri uri) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("SCAN COMPLETED: " + path);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });

慕容森

嘿,我发现了如何用一个非常简单的代码来做到这一点。只需调用以下代码:sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));这应该触发mediascanner。
打开App,查看更多内容
随时随地看视频慕课网APP