java - 隐藏时从图库中删除图像缩略图

标签 java android

这个问题之前已经被问过(不是特别像这样),但还没有一个 All Exclusive 的答案。所以我们试图在这里找到最好的解决方案。我正在开发一个应用程序,在我的应用程序中,我通过将其文件移动到一个名为 .myPic 的目录来隐藏一个名为 myPic 的目录。当我隐藏我的照片时,它的缩略图仍在画廊中。我找到了 3 个解决方案:

第一个解决方案:

像这样使用 ACTION_MEDIA_MOUNTED 广播:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

此代码的问题在于它占用大量资源,最重要的是它自 android 4.4 起被阻止。所以使用这种方法在图库中添加 10 张图片是不合理的。所以它不是一个全排他的方法。也使用 ACTION_MEDIA_SCANNER_SCAN_FILE 在 android 4.4 上也不起作用

第二种解决方案:

使用 MediaScannerConnection。所以我创建了一个 for 循环并传递我隐藏的每个文件的旧地址。这是我的 MediaScannerConnection 函数:

private void scanFile(File file) {
    // Tell the media scanner about the new file so that it is
    // immediately available to the user.
    MediaScannerConnection.scanFile(this,new String[] { file.toString() }, null,
        new MediaScannerConnection.OnScanCompletedListener() {
            public void onScanCompleted(String path, Uri uri) {
                Log.i("ExternalStorage", "Scanned " + path + ":");
                Log.i("ExternalStorage", "-> uri=" + uri);
            }
        });
}

关于 MediaScannerConnection 的事情是它仅在文件存在时才有效。所以假设我在 myPic 目录中有一张名为 1.jpg 的图片。使用这个类,我可以立即将 1.jpg 添加到我的画廊,但是当我将 1.jpg 移动到 .myPic 目录并扫描旧的1.jpg 的路径没有任何反应。 logcat 说这个文件不存在。所以 MediaScannerConnection 只将文件添加到画廊。如果我将 1.jpg 的新路径传递给 MediaScannerConnection 会怎样?好吧,它将 .myPic 目录中的 1.jpg 添加到画廊,这正是我想要的不是。所以再次不是完全排他的方法

第三种解决方案:

使用 getContentResolver()。所以对于删除缩略图,这种方法可能是最终的解决方案。所以我写了打击代码。在每个循环中,我都会检索图像的路径并将其传递给 getContentResolver().delete(Uri.parse(path),null,null)。这是代码:

File myPic = new File(Environment.getExternalStorageDirectory()+"/myPic");
File myPicHide = new File(Environment.getExternalStorageDirectory()+"/.myPic");
if (!(myPicHide.exists()) & !(myPicHide.isDirectory())) {
    myPicHide.mkdirs();
};
if (myPic.isDirectory()) {
    String[] childeren = myPic.list();
    if (childeren.length > 0) {
        for (int i = 0; i < childeren.length; i++) {
            String fileName = childeren[i];
            File from = new File(Environment.getExternalStorageDirectory()+"/myPic"+fileName);
            File to = new File(Environment.getExternalStorageDirectory()+"/.myPic"+fileName);
            from.renameTo(to);
            try {
                String path = from.toString();

                getContentResolver().delete(Uri.parse(path),null,null);
            } catch(Exception e) {
                Log.d("Rename", "Error happened");
            }
        }
    }
} else { 
    Toast.makeText(getApplicationContext(), "myPic directory not found", Toast.LENGTH_LONG).show();
}

但它也不起作用,我的文件的缩略图仍然显示在厨房中。那么我是否以错误的方式使用 getContentResolver() ?对于已删除文件缩略图出现在图库中的情况,这可能是所有独占方法。我有我的文件路径,我只需要从媒体存储内容提供商中删除它。

更新: 事实证明,在第三个解决方案中使用 Uri.parse(path) 是错误的。 image Uri 以 content:// 开头,可以通过 MediaScannerConnection 检索。所以我创建了一个名为 imageInGalleryUriUri 并为其分配 null 值。使用我的 scanFile 函数,我不时更改它的值并将它的值传递给 getContentResolver()。这是代码:

    boolean whereIsMediaState = true;
    Uri imageInGalleryUri = null;
    
    File myPic = new File(Environment.getExternalStorageDirectory()+"/myPic");
    File myPicHide = new File(Environment.getExternalStorageDirectory()+"/.myPic");
    if (!(myPicHide.exists()) & !(myPicHide.isDirectory())) {
        myPicHide.mkdirs();
    };
    if (myPic.isDirectory()) {
        String[] childeren = myPic.list();
        if (childeren.length > 0) {
            for (int i = 0; i < childeren.length; i++) {
                String fileName = childeren[i];
                File from = new File(Environment.getExternalStorageDirectory()+"/myPic"+fileName);
                scanFile(from);
                File to = new File(Environment.getExternalStorageDirectory()+"/.myPic"+fileName);
                from.renameTo(to);
                if (to.isFile()){
                try {
                    getContentResolver().delete(imageInGalleryUri,null,null);}
                catch(Exception e) {
                    Log.d("Rename", "Error happened");
                }
            }
        }
    } else { 
        Toast.makeText(getApplicationContext(), "myPic directory not found", Toast.LENGTH_LONG).show();
    }
        
        private void scanFile(File file) {
            // Tell the media scanner about the new file so that it is
            // immediately available to the user.
            MediaScannerConnection.scanFile(this,new String[] { file.toString() }, null,
            new MediaScannerConnection.OnScanCompletedListener() {
            public void onScanCompleted(String path, Uri uri) {
            Log.i("ExternalStorage", "Scanned " + path + ":");
            Log.i("ExternalStorage", "-> uri=" + uri);
            imageInGalleryUri = uri;
            }
            });
        }

我尝试了代码,但它只检测到第一张图像并将其从图库中删除,但不会影响其他图像。我不知道为什么。有什么想法吗?

提前感谢您的帮助

最佳答案

。之前的文件夹只是让它不可见。但是有办法说根本不要使用这个文件夹来画廊。 请尝试将名为“.nomedia”的空文件放入您的文件夹中。

关于java - 隐藏时从图库中删除图像缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29843846/

相关文章:

java - 在java中从SQL数据库调用Enum值

java - 如何在不重新启动服务的情况下重新加载 Coldfusion 中的打印机?

android - Flutter multi_image_picker相机选项未在iOS中显示。当我已经启用摄像头并将权限写入info.plist中时

android - 如何制作firebase实时数据库用户的私有(private)节点?

android - 在 Android 上保存 foursquare oauth token

Android - 如何在另一个 Activity 期间暂停线程?

java - 将首字母更改为大写

java spring无尽的并发工作

java - 属性的级别值错误,无法为 java.util.logging.FileHandler 设置级别

android - Tegra 平板上的 NDK 调试