title: When to delete file shared with external apps by using Intent.ACTION_SEND in Android
body: <p>In my application i allow users to share files with external apps by using <strong>Intent.ACTION_SEND</strong>. My files are encrypted so before I can share one of those files, I need to decrypt it and store it in application internal storage. After that I share file Uri to send a file.<strong>For security issues I would like to remove the file from internal storage after external application has used it, or user cancelled operation</strong>.</p>
  
  <p><strong>Brief</strong> : in my project i decrypt the selected files and save in to internal storage location Temp folder/directory. after that generate arraylist of uris for that files and share arraylist of 'usri's. </p>
  
  <p>here main problem is delete the temp folder after success/failure to share files.</p>
  
  <p>I use below code for sharing files</p>
  
  <pre><code>private void startShareIntent(ArrayList&lt;Uri&gt; uris) {
      Intent share = new Intent(Intent.ACTION_SEND_MULTIPLE);
      share.setType("audio/*");
      share.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
      startActivityForResult(Intent.createChooser(share, "Share audio File(s)"),SHARE_AUDIO_FILES_CODE);
  }
  </code></pre>
  
  <p>for getting onActivity result</p>
  
  <pre><code>protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (resultCode == RESULT_OK) {
          if(requestCode==SHARE_AUDIO_FILES_CODE)
          {
              //TODO delete decrypted files when success
           }
          }
      if(resultCode==RESULT_CANCELED)
      {
          //TODO delete decrypted files when failure.
      }
  }
  </code></pre>
  
  <p>But here always resultCode return RESULT_CANCELED only. and its called before completed of sharing files.</p>