【问题标题】:Delete images from SD card as well as from gallery从 SD 卡和图库中删除图像
【发布时间】:2012-04-03 13:33:14
【问题描述】:

我用这种方式将一些图片保存在 SD 卡上:

File dir = new File(fullPath);
                        if (!dir.exists()) {
                            dir.mkdirs();
                        }
                        String id = Integer.toString(i+1);
                        OutputStream fOut = null;
                        File file = new File(fullPath, id);
                        file.createNewFile();
                        fOut = new FileOutputStream(file);

                        // 100 means no compression, the lower you go, the stronger the compression
                        bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                        fOut.flush();
                        fOut.close();

                        MediaStore.Images.Media.insertImage(this.getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());

但是,当我从 SD 卡中删除这些图像时,它们会保留在图库中。

File path = new File(path);
            File[] lstFile;

            if(path.exists()){
                lstFile = path.listFiles();
                for(int i =0; i<lstFile.length;i++){
                    File file = lstFile[i];
                    file.delete();
                }
                path.delete(); 
            }

这些图片怎么会留在图库中,我该如何删除它们?有没有办法一开始就避免将这些图像保存在图库中?

【问题讨论】:

  • MediaStore 创建的缩略图是什么。难道它不是你删除文件的“记忆”所在的地方吗?

标签: android image sd-card image-gallery delete-file


【解决方案1】:

哦!没注意

“有没有办法一开始就避免将这些图像保存在图库中?”

是的,有。删除

 MediaStore.Images.Media.insertImage(this.getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());

并仅使用 File 类保存文件,而不是 MediaStore 类,其目的正是帮助管理画廊。

【讨论】:

  • 所以基本上我只需要删除 MediaStore.Images.Media.insertImage(this.getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());它不会进入画廊?
  • 我想是的,我真的不擅长输出流等。注释掉,编译,检查你的文件是否被保存,如果没有,问别人;)
  • 就是这样。我真的应该更加小心我使用的东西。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-07
  • 2011-08-06
  • 1970-01-01
  • 1970-01-01
  • 2013-03-24
  • 1970-01-01
相关资源
最近更新 更多