从SD卡和图库中删除图像

时间:2012-04-03 13:33:15

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

我正在以这种方式在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(); 
            }

为什么那些图像留在图库中,我该如何删除它们?有没有办法首先避免这些图像保存在图库中?

1 个答案:

答案 0 :(得分:1)

哦!我没注意到

“有没有办法首先避免这些图像保存在图库中?”

是的,有。删除

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

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

相关问题