android - 防止媒体扫描仪扫描在 Android 上使用 DownloadManager 下载的 JPG? (开发商)

标签 android android-mediascanner android-download-manager

我的应用程序使用 DownloadManager 从网站下载 jpg,并将它们存储在/sdcard/Android/name.app/目录中,以在应用程序的多个 fragment 中使用。下载效果很好,但是 jpg 在 Gallery 应用程序中可见,这是我不想要的,因为它们只是我的应用程序的 Assets 。我不知道如何让它们不显示在图库应用程序中。我什至尝试将 .nomedia 文件添加到创建它们的目录中。任何帮助,将不胜感激!

    DownloadManager myDownloadManager = (DownloadManager) myActivity.getSystemService(Context.DOWNLOAD_SERVICE);
    Request myRequest = new Request(Uri.parse(episodeArtLink));
    myRequest.setNotificationVisibility(Request.VISIBILITY_HIDDEN);
    myRequest.setVisibleInDownloadsUi(false);
    myRequest.setDestinationInExternalFilesDir(myActivity, Environment.DIRECTORY_DOWNLOADS, myFilename);
    myDownloadManager.enqueue(myRequest);

问题似乎是因为 MediaScanner 正在将图像添加到系统数据库,即使我没有要求它这样做。

我已经创建了一个演示问题的测试应用程序。如果您单击“下载”按钮,Google Logo 将显示在应用程序中。如果您进入 Gallery 应用程序,您还应该在名为 Download 的图库中看到 Google Logo 。如果您转到设置 -> 应用程序并清除图库和媒体存储的数据,那么 Google Logo 将消失并且不会再次出现在图库中,因为该文件位于/sdcard/Android/data/下,该文件受.nomedia 文件。您可以转到/sdcard/Android/data/com.example.downloadmanagertest/files/Download/以验证图像仍在手机上。即使在重新启动后,如果您清除图库和媒体存储的数据,图像也不会显示在图库中。我已经在运行 4.2.2 的 Nexus 4 和 Nexus 7 上对此进行了测试。

来源:https://dl.dropboxusercontent.com/u/114414/DownloadManagerTest.zip

最佳答案

解决方案

媒体扫描器最终迟早会扫描媒体卡以查找媒体并对它们进行索引。第一个直观的步骤是欺骗扫描仪不知道该文件是媒体文件。这可以通过不带扩展名保存图像来实现。一个简单的解决方案是将 notkevin 附加到文件名的末尾。简单地:

代替

 myRequest.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, Uri.parse(downloadLink).getLastPathSegment());

使用这个

 String filename = Uri.parse(downloadLink).getLastPathSegment() + "notkevin";
 myRequest.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, filename);

但是,这对您的情况还不够,因为您使用的是 DownloadManager(只是猜测)。媒体扫描仪以某种方式知道下载的项目以及它是图像的事实,因此对其进行索引。要禁止它,请像这样使用 setMimeType:

myRequest.setMimeType("application/octet-stream");

PS:除了将您或我的名字添加到文件名末尾之外,还有其他方法可以更改扩展名。例如,您可以散列文件名

String filename = hash(Uri.parse(downloadLink).getLastPathSegment());

public String hash(String victim) throws NoSuchAlgorithmException
{
    MessageDigest md = MessageDigest.getInstance("SHA1");
    md.reset();
    byte[] buffer = victim.getBytes();
    md.update(buffer);
    byte[] digest = md.digest();

    StringBuilder hexStr = new StringBuilder();
    for (int i = 0; i < digest.length; i++) {
        hexStr.append(Integer.toString( ( digest[i] & 0xff ) + 0x100, 16).substring( 1 ));
    }
    return hexStr.toString();
}

关于android - 防止媒体扫描仪扫描在 Android 上使用 DownloadManager 下载的 JPG? (开发商),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16391670/

相关文章:

android - 从购买对象 Play Billing v5 获取特定优惠

android - 下载管理器在使用平台默认值的 Android Pie 9.0 NetworkSecurityConfig : No Network Security Config specified, 中不起作用

Android:DownloadManager - 通知粘贴

android - 如何引用 XML 中的字符串数组元素 (android)

android - Firebase 云消息传递设备 ID : multiple user in same app in same device

android - 如何使用元数据将媒体添加到 Android 4.4 KitKat SD 卡上的 MediaStore

java - 媒体扫描仪完成其事件后如何响应

Android 内容解析器找不到新图像

android - 在 Android 中下载和暂停、恢复大文件的最佳方式?

android - 在 Android 4.4 和 7.0 上删除短信