如何在运行Android的媒体扫描 [英] how to run media scanner in android

查看:270
本文介绍了如何在运行Android的媒体扫描的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的机器人。我想运行介质扫描,同时拍摄图像。捕获后,图像就在网格视图更新。为此,我需要运行介质扫描仪。我发现了两个解决方案来执行媒体扫描一个是在播放事件,另一个是运行媒体扫描器类。我认为,在冰淇淋sandiwich(4.0)介质的扫描仪类是introduced.Before版本需要设置广播事件运行媒体扫描仪。

I am new to android. I want run the media scanner while capturing the image. After capturing, the image it is updated in grid view. For that I need to run media scanner. I found two solutions to run media scanner one is the broadcast event and other one is running media scanner class. I think in ice cream sandiwich (4.0) media scanner class is introduced.Before versions need to set broadcast event for running media scanner.

CCAN任何一个指导我如何运行媒体扫描仪在右way.it成为我真头疼。请帮我。

Ccan any one guide me how to run media scanner in right way.it becomes me real headache. Please help me.

推荐答案

我发现,最好(快/最小开销)来运行特定文件的媒体扫描(VS运行它来扫描介质的所有文件),如果你知道文件名。下面是我使用的方法:

I have found it best (faster/least overhead) to run media scanner on a specific file (vs running it to scan all files for media), if you know the filename. Here's the method I use:

/**
 * Sends a broadcast to have the media scanner scan a file
 * 
 * @param path
 *            the file to scan
 */
private void scanMedia(String path) {
    File file = new File(path);
    Uri uri = Uri.fromFile(file);
    Intent scanFileIntent = new Intent(
            Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
    sendBroadcast(scanFileIntent);
}

当需要对多个文件运行(初始化多个图像的应用程序时,如),我把新的图像文件名的集合,而初始化,然后运行的每一个新的图像文件,上面的方法。在下面的code, addToScanList 添加要扫描的文件到的ArrayList< T> scanMediaFiles 用于启动扫描数组中的每个文件。

When needing to run on multiple files (such as when initializing an app with multiple images), I keep a collection of the new images filenames while initializing, and then run the above method for each new image file. In the below code, addToScanList adds the files to scan to an ArrayList<T>, and scanMediaFiles is used to initiate a scan for each file in the array.

private ArrayList<String> mFilesToScan;

/**
 * Adds to the list of paths to scan when a media scan is started.
 * 
 * @see {@link #scanMediaFiles()}
 * @param path
 */
private void addToScanList(String path) {
    if (mFilesToScan == null)
        mFilesToScan = new ArrayList<String>();
    mFilesToScan.add(path);
}

/**
 * Initiates a media scan of each of the files added to the scan list.
 * 
 * @see {@see #addToScanList(String)}
 */
private void scanMediaFiles() {
    if ((mFilesToScan != null) && (!mFilesToScan.isEmpty())) {
        for (String path : mFilesToScan) {
            scanMedia(path);
        }
        mFilesToScan.clear();
    } else {
        Log.e(TAG, "Media scan requested when nothing to scan");
    }
}

这篇关于如何在运行Android的媒体扫描的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆