本文介绍了如何正确通知MediaScanner有关的文件夹,甚至在Nexus /奇巧设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个应用程序,下载一些图片,从互联网到一个特定的文件夹被定义为:

I'm making an app that downloads some images from the Internet into a specific folder that is defined as:

final String pathToPutFiles = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
            + File.separator + DOWNLOAD_FOLDER_NAME;

我希望做的美术馆,在应用程序,显示媒体,将这些图像的通知,并能够向他们展示。

I wish to make the gallery and any app that shows media, to be notified of those images and be able to show them.

可悲的是我有在Nexus 4,它有奇巧的版本(4.4.2是精确的)问题。有时工作,有时没有。

Sadly I'm having problems on Nexus 4, which has Kitkat version (4.4.2 to be exact). Sometimes it works and sometimes it doesn't.

有很多关于这个问题的帖子,我尝试了很多人,并使用一个简单的文件夹路径没有工作:

There are plenty of posts about this issue, and I've tried many of them and none worked using a simple folder path:

  • 使用意向的意图。ACTION_MEDIA_SCANNER_SCAN_FILE如图所示here 。这并没有在任何设备上工作,
  • 创建MediaScannerConnectionClient的一个实例,调用扫描一旦连接已完成,如图 这里 here 。不知道有什么不对的,因为它成功连接。
  • 呼叫scanFile ,如图 此处 ,有和没有一个听众。这工作正常银河S3,但不能在Nexus 4,这是我选择的事,但我注意到,它不能用一个单一的文件夹路径工作。相反,它需要的文件数组来通知。

这是我做了什么(再次,它工作正常银河S3拥有的Andr​​oid 4.3,但有时并不在Nexus 4具有的Andr​​oid 4.4.2工作):

This is what I've done (again, it works fine on Galaxy S3 which has Android 4.3, but sometimes doesn't work on Nexus 4 which has Android 4.4.2) :

protected void notifyMediaScannerOfNewImageFiles(final String folderPathToScan) {
        final ArrayList<String> newFilesToScan = new ArrayList<String>();
        preparePaths(folderPathToScan, newFilesToScan, true);
        final String[] newFilesToScanArr = newFilesToScan.toArray(new String[newFilesToScan.size()]);
        MediaScannerConnection.scanFile(getApplicationContext(), newFilesToScanArr, null, null);
}

public static void preparePathsForMediaScanning(final String folderPathToScan, final Collection<String> pathsToFill,
        final boolean recursive) {
    if (TextUtils.isEmpty(folderPathToScan))
        return;
    final File folderToScan = new File(folderPathToScan);
    if (!folderToScan.exists())
        return;
    pathsToFill.add(folderPathToScan);
    if (!folderToScan.isDirectory())
        return;
    final File[] childrenFiles = folderToScan.listFiles();
    if (childrenFiles == null || childrenFiles.length == 0)
        return;
    for (final File childFile : childrenFiles) {
        if (childFile.exists()) {
            final String childFilePath = childFile.getAbsolutePath();
            pathsToFill.add(childFilePath);
            if (recursive && childFile.isDirectory())
                preparePathsForMediaScanning(childFilePath, pathsToFill, true);
        }
    }
}

问题

整个文件夹的

我应该如何通知扫描?

The question

How should I notify of an entire folder to scan?

为什么奇巧做这个方法的工作只是有时?难道仅仅的Nexus设备,或过于其他设备?

Why does this method work only sometimes on Kitkat ? Is it just Nexus devices, or other devices too?

应该用什么来支持所有的设备和Android版本?

What should be used to support all devices and Android versions?

推荐答案

我现在拍着我的头,围绕这一问题进行了大量的时间。由于贴在这太anwser ,现在看来似乎是发生在奇巧的错误。

I have banged my head around this problem for a good deal of time by now. As posted on this SO anwser, it seems like it is a bug that occurs on KitKat.

正在跟踪两个外部的问题和国内的Andr​​oid团队。

The issue is being tracked both externally and internally by the Android team.

在此期间,这是我发现了:http://stackoverflow.com/a/23078323/1367571

In the meantime, this is what I've found out: http://stackoverflow.com/a/23078323/1367571

这篇关于如何正确通知MediaScanner有关的文件夹,甚至在Nexus /奇巧设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 04:01