0

I want to delete a file (pdf file) I did this :

boolean deleted = filesList.get(pos).delete();

But when I look in my phone I see this file , but my application doesn't see this file

Blackbelt
  • 156,034
  • 29
  • 297
  • 305

2 Answers2

0

Your code doesn't delete a file from the file system. It just deletes an element from the list. Check this for more delete file in sdcard

To delete a file from the file system, first you need to provide Permission to read and write local storage in your AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">     
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Then, in your code,

String path =  "/mnt/sdcard/test.pdf";
File mFile = new File(path);
mFile.delete();
vss
  • 1,093
  • 1
  • 20
  • 33
ylmzekrm1223
  • 103
  • 5
  • 15
0

To delete a file from a directory, you can use this method:

public static void deleteFile(File directory, String fileName) {

    if (directory.isDirectory()) {
        for(File file : directory.listFiles()) {

            if (file.getName().contains(fileName)) {

                if (file.isFile()) {
                    if (file.exists()) {
                        file.delete();
                    }
                }
            }
        }
    }
}

And if you want to delete the entire directory:

public static void deleteDirectory(File directory) {
    if (directory.isDirectory())
        for (File child : directory.listFiles())
            deleteDirectory(child);

    directory.delete();
}

As mentioned by ylmzekrm1223, you should provide permissions to read and write storage in your AndroidManifest.xml, before attempting to delete a file or a directory.

vss
  • 1,093
  • 1
  • 20
  • 33