How to delete internal storage file in android?

后端 未结 8 1989
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 06:27

I have used the Android internal storage to save a file for my application (using openFileOutput) but I would like to delete that file, is it possible and how?<

相关标签:
8条回答
  • 2020-11-30 06:47
    File dir = getFilesDir();
    File file = new File(dir, "my_filename");
    boolean deleted = file.delete();
    
    0 讨论(0)
  • 2020-11-30 06:47

    Use delete method of File

    0 讨论(0)
  • 2020-11-30 06:47

    Another alternative in Kotlin

    val file: File = context.getFileStreamPath("file_name")
    val deleted: Boolean = file.delete()
    
    0 讨论(0)
  • 2020-11-30 06:54

    If you want to delete all files from a folder then use the following function:

    private void deleteTempFolder(String dir) {
            File myDir = new File(Environment.getExternalStorageDirectory() + "/"+dir);
            if (myDir.isDirectory()) {
                String[] children = myDir.list();
                for (int i = 0; i < children.length; i++) {
                    new File(myDir, children[i]).delete();
                }
            }
        }
    

    Folder must be present on storage. If not we can check one more codition for it.

      if (myDir.exists() && myDir.isDirectory()) {
    //write same defination for it.
    }
    
    0 讨论(0)
  • 2020-11-30 07:01
    new File(mUri.toString).delete(); 
    
    0 讨论(0)
  • 2020-11-30 07:02

    I know this is a bit of an oldie, but the docs say to use:

    deleteFile("filename");
    

    rather than:

    File.delete();
    

    Which if you are already using:

    getFilesDir();
    

    kind of makes sense.

    0 讨论(0)
提交回复
热议问题