Search Unity

How to delete a file in android?

Discussion in 'Scripting' started by DavidFerrG, Oct 13, 2017.

  1. DavidFerrG

    DavidFerrG

    Joined:
    Jun 1, 2017
    Posts:
    4
    Hi, im developing a Android/iOS videogame in Unity. I need to delete a file, i have the path. Im using the next code:

    Code (CSharp):
    1.     public void DeleteElement (string path)
    2.     {
    3.         File.Delete(ruta);
    4.     }
    When I delete a file in android, I can see from the android file browser that it is no longer in the folder where it was before, but when, from my Windows computer, I check that folder with the File Explorer, it is still there, although it can not be opened.

    Why is the file not completely deleted? How do I safely remove it?
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    So you are deleting the file in the Windows file system? Or are you using Windows File Explorer to browse your Android file system. When you exit Unity, is the file still there?
     
  3. DavidFerrG

    DavidFerrG

    Joined:
    Jun 1, 2017
    Posts:
    4
    I'm using Windows File Explorer to browse the Android device file system and check if the Unity app actually deletes the file using "File.Delete(path)". In Android device file system the file inst there. In Windows File Explorer I still see the file although it is unusable. The file is in Movies folder of the Android file system.
     
  4. DavidFerrG

    DavidFerrG

    Joined:
    Jun 1, 2017
    Posts:
    4
    It Looks like the Android file System keep the files deleted and will removing them by restarting the device or some other way. I will not worry about that anymore. Thanks
     
  5. tirange

    tirange

    Joined:
    Jun 14, 2018
    Posts:
    1
    Usually, before the deleted file being overwritten, it will be still saved. I think you need to use android data eraser tool to help you delete the file in Android permanently.
     
  6. TeotiGraphix

    TeotiGraphix

    Joined:
    Jan 11, 2011
    Posts:
    145
    I know this is old but you have not updated the Media cache in Android. Thus the real file system has not file but since Windows reads from a cache, since you didn't tell Android to dirty the cache, Windows still thinks there is a file there.

    Restarting your device will dirty the cache and all will be well.

    Sorry, the code for users down the road;

    Code (CSharp):
    1. sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(imageAdded)));