从内部存储中删除文件

100

我正在尝试删除存储在内部存储中的图像。 到目前为止,我想到了这个:

File dir = getFilesDir();
File file = new File(dir, id+".jpg");
boolean deleted = file.delete();

这是来自另一个问题的答案,该答案已经被 回答了:

File dir = getFilesDir();
File file = new File(dir, "my_filename");
boolean deleted = file.delete();

我的示例始终返回false。我可以在Eclipse的DDMS中看到文件fx 2930.jpg

8个回答

142

getFilesDir()方法不知道为什么没起作用。 使用另一种方法可以返回完整的路径和文件名,这样就得到了所需的结果。这里是代码:

File file = new File(inputHandle.getImgPath(id));
boolean deleted = file.delete();

1
inputHandle.getImgPath(id)filePath - Pratik Butani

48

尝试使用Contex.deletFile()的变体,但没有成功。以下是看起来有效的方法。 - Crunch
@user661543 ih.getImgPath() 返回的是完整路径是什么?你传递给删除文件的参数是什么?如果上面的方法不起作用,那么最有可能是将您的文件存储在应用程序包之外。或者您可能已经传递了错误的文件名作为参数。 - Konstantin Burov
+1 这是最简单的方法,可以删除通过 new OutputStreamWriter(context.openFileOutput(fileName, Context.MODE_PRIVATE)).write(data); 写入的文件。 - Eugen

21

Java

File file = new File(photoPath);
file.delete();

MediaScannerConnection.scanFile(context,
                new String[]{file.toString()},
                new String[]{file.getName()},null);

Kotlin

val file = File(photoPath) 
file.delete()

MediaScannerConnection.scanFile(context, arrayOf(file.toString()),
      arrayOf(file.getName()), null)

15
String dir = getFilesDir().getAbsolutePath();
File f0 = new File(dir, "myFile");
boolean d0 = f0.delete(); 
Log.w("Delete Check", "File deleted: " + dir + "/myFile " + d0);

这段代码File dir = getFilesDir();不能工作,因为它是对文件对象的请求。你试图获取声明路径的字符串,所以你需要将'dir'声明为一个字符串,然后请求访问该信息的构造函数返回目录的绝对路径字符串形式。


11
您还可以使用:file.getCanonicalFile().delete();

4
File file = new File(getFilePath(imageUri.getValue())); 
boolean b = file.delete();

在我的情况下不起作用。

boolean b = file.delete();                 // returns false
boolean b = file.getAbsolutePath.delete(); // returns false 

始终返回 false。

通过使用以下代码解决了该问题:

ContentResolver contentResolver = getContentResolver();
contentResolver.delete(uriDelete, null, null);

我知道我不能留下“感谢”的信息,但是......我非常爱你!!!谢谢! - kinghomer

2

你尝试过 getFilesDir().getAbsolutePath() 吗?

看起来你通过使用完整路径初始化文件对象解决了问题。我相信这也可以解决问题。


0
>     2019-11-12 20:05:50.178 27764-27764/com.strba.myapplicationx I/File: /storage/emulated/0/Android/data/com.strba.myapplicationx/files/Readings/JPEG_20191112_200550_4444350520538787768.jpg//file when it was created

2019-11-12 20:05:58.801 27764-27764/com.strba.myapplicationx I/File: content://com.strba.myapplicationx.fileprovider/my_images/JPEG_20191112_200550_4444350520538787768.jpg //same file when trying to delete it

解决方案1:

              Uri uriDelete=Uri.parse (adapter.getNoteAt (viewHolder.getAdapterPosition ()).getImageuri ());//getter getImageuri on my object from adapter that returns String with content uri

在这里我初始化Content Resolver,并使用传递的URI参数将其删除

            ContentResolver contentResolver = getContentResolver ();
            contentResolver.delete (uriDelete,null ,null );

solution2(我的第一个解决方案-从头开始,我现在知道了):内容解析器已存在...

              String path = "/storage/emulated/0/Android/data/com.strba.myapplicationx/files/Readings/" +
                    adapter.getNoteAt (viewHolder.getAdapterPosition ()).getImageuri ().substring (58);

            File file = new File (path);
            if (file != null) {
                file.delete ();
            }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接