title: Delete a File from the apps private storage directory - Android
body: <p>I am having a bit of trouble trying to delete a file from the applications private storage directory that I have programmatically created and compressed.</p>
  
  <p>The problem I am facing is that it's not recognising that the file exists when I call <em>file.exists()</em>, and so it will not delete the file.</p>
  
  <p>This is the code I'm using to delete the file</p>
  
  <pre><code>public static void deleteImageFromPrivateArea( final String fileUri )
    {
      final File file;
      boolean isFileDeleted;
      if ( isFilePathPrefixPresent( fileUri ) )
      {//file name checking
        file = new File( fileUri );
      }
      else
      {
        file = new File( "file://" + fileUri );
      }
  
      if ( file.exists() )
      {
        isFileDeleted = file.delete();
        if ( ! isFileDeleted )
        {
          throw new IllegalStateException( "File was not deleted" );
        }
      }
    }
  </code></pre>
  
  <ol>
  <li><p>To clear things up, I know that the fileUri I'm passing through is the location of a file that currently exists. I have checked with a filemanager app.</p></li>
  <li><p>I have the permissions set up in the manifests xml.</p></li>
  <li><p>What I believe is happening is that this code is getting the path of the currently existing file, and setting up a new file but not writing the data to it thus this new file does not exist in android.</p></li>
  </ol>
  
  <p>So what I think I need to do is get the existing file (maybe not in the form of a Uri, but the actual file) and then call .delete() as then the file would exist.</p>
  
  <p>Or</p>
  
  <p>After setting up this new file with the fileUri, write the data to it using an OutputfileStream - this would not be my preferred method though.</p>
  
  <p>-but I'm not entirely sure.</p>