preventing媒体从扫描与下载管理器在Android上下载的JPG文件扫描仪? (开发者) [英] Preventing Media Scanner from Scanning JPGs Downloaded with the DownloadManager on Android? (Developer)

查看:188
本文介绍了preventing媒体从扫描与下载管理器在Android上下载的JPG文件扫描仪? (开发者)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序下载的JPG文件。下载的伟大工程然而,JPG文件在Gallery应用程序,这是我不希望,因为他们只是资产,我的应用程序可见。我无法弄清楚如何让他们在Gallery应用程序显示不出来。我甚至尝试添加.nomedia文件的目录创建它们。任何帮助将是AP preciated!

My application downloads jpgs from a website using DownloadManager and store them in the /sdcard/Android/name.app/ directory to be used in several Fragments in the application. The download works great however, the jpgs are visible in the Gallery application, which I don't want since they are just assets for my application. I cannot figure out how to have them not show up in the Gallery application. I even tried adding .nomedia files to the directories are they are created. Any help would be appreciated!

    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是添加图像到即使我没有要求它到系统数据库中造成的。

The issues seem to be caused because MediaScanner is adding the Images to the system database even though I have not asked it to.

我创建了一个测试程序演示该问题。如果你点击下载按钮谷歌标志将在应用程序中显示。如果你进入图库应用程序,你也应该看到,在一个画廊命名下载谷歌标志。如果你去设置 - >应用和清除数据,画廊和媒体存储,然后谷歌标志将消失,不会再图库显示,因为该文件在/ SD卡/ Android的/数据/这是受保护.nomedia文件。你可以去/sdcard/Android/data/com.example.downloadmanagertest/files/Download/验证图像仍然在手机上。即使在重新启动后图像不画廊,即使你清除数据的画廊和媒体存储。我有两个一个的Nexus 4和运行4.2.2的Nexus 7测试这一点。

I have created a test app that demonstrates the problem. If you click the Download button the Google logo will show up in the app. If you go into the Gallery app you should also see the Google logo in a gallery named Download. If you go to Settings -> Apps and Clear the Data for Gallery and Media Storage then the Google logo will be gone and won't show up again in Gallery because the file is under /sdcard/Android/data/ which is protected by the .nomedia file. You can go to /sdcard/Android/data/com.example.downloadmanagertest/files/Download/ to verify that the images are still on the phone. Even after a reboot the images don't show up in Gallery if you clear the Data for Gallery and Media Storage. I have tested this on both a Nexus 4 and a Nexus 7 running 4.2.2.

来源: https://dl.dropboxusercontent.com/u/114414/DownloadManagerTest。拉链

推荐答案

解决方案

Solution

媒体扫描仪将最终迟早扫描媒体和指数它们介质卡。第一个直观的步骤是扫描仪欺骗不知道该文件是一个媒体文件。这可以通过保存图像不带扩展来实现。一个简单的解决办法是的 notkevin 的追加到文件名末尾。简单地说:

The media scanner will eventually sooner or later scan the media card for media and index them. The first intuitive step is to trick the Scanner into not knowing that the file is a media file. This can be achieved by saving the images without the extension. An easy solution would be to append notkevin to the end of the filename. Simply:

而不是

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

使用此

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

但是,由于您使用的下载管理器(只是猜测),这是不是你的情况就足够了。媒体扫描器某种程度上知道下载的项目和事实,即它是一个图像,因此,索引它。要禁止它,使用 setMimeType 是这样的:

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


PS:有办法改变不是增加你的还是我的名字到文件名末尾其他的扩展。例如,您可以哈希文件名


PS: There are ways to change the extension other than adding your or my name to the end of the filename. For example, you could hash the filename

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();
}

这篇关于preventing媒体从扫描与下载管理器在Android上下载的JPG文件扫描仪? (开发者)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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