Skip to content Skip to sidebar Skip to footer

Delete File After Sent To Mail As Intent.action_send

I have following code : Uri screenshotUri = Uri.fromFile(file); Intent intent = new Intent(Intent.ACTION_SEND); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Solution 1:

If your question is how do you know when to delete the file, then the answer is you can't know.

The method I use is to keep the file in the application's cache directory (either internal or external). So it'll be automatically deleted by Android when the device runs short of storage. As a good practice however, I first delete all existing files in the cache before sharing a new file.

To actually delete the file, refer to @Sahil's answer

Solution 2:

First Refer @Dheeraj V.S. answer.

Ways to delete the files

  1. You can delete these files using service which run in background. Service check whether folder contain any file then write logic in service such that it will delete the files.

  2. You can delete these files on starting the your application. Means if any files exist in particular folder so in starting welcome activity you can put logic to delete file.

//To delete the hidden files

try {
      new Helper().deleteFromExternalStorage(".photo.jpg");
}
catch(Exception e){
      Log.v("APP","Exception while deleting file");
}

Method to delete file from external storage

publicvoiddeleteFromExternalStorage(String fileName) {
  String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/directoryname";
  try
  {
    File file = newFile(fullPath, fileName);
    if(file.exists())
        file.delete();
  }
  catch (Exception e)
  {
    Log.e("APP", "Exception while deleting file " + e.getMessage());
  }
}

Solution 3:

You can use following code to delete file from external storage, after the Intent,

Filefile=newFile(selectedFilePath);
booleandeleted= file.delete();

where selectedFilePath is the path of the file you want to delete - for example:

/sdcard/YourCustomDirectory/ExampleFile.mp3

Solution 4:

When you open e mailbox according to activity life cycle, your current Activity move on onPause() when you return in your activity then On Resume method will call so write the blow code on your on Resume method.This trick solved my problem.

protectedvoidonResume() {
    // TODO Auto-generated method stubFile file= newFile(android.os.Environment.getExternalStorageDirectory().toString()+ "/akanksha" + ".png");
    if(file.exists())
    {
         file.delete();
    }
    super.onResume();
}

Solution 5:

Another potential answer would be to create a new thread when your app resumes, immediately mark the current time, sleep the thread for however long you feel is reasonable for the file to be sent, and when the thread resumes, only delete files created before the previously marked time. This will give you the ability to only delete what was in the storage location at the time your app was resumed, but also give time to gmail to get the email out. Lastly it addresses the situation where you email a set of files, then email a second set, but when you are cleaning up the first set, you delete the second set before it gets sent out, this guarantees that you only delete the files you put out in the directory at the time the email was sent. Code snippet: (I'm using C#/Xamarin, but you should get the idea)

publicstaticvoidClearTempFiles()
{
    Task.Run(() =>
    {

    try
    {
        DateTime threadStartTime = DateTime.UtcNow;
        await Task.Delay(TimeSpan.FromMinutes(DeletionDelayMinutes));
        DirectoryInfo tempFileDir = new DirectoryInfo(TempFilePath);
        FileInfo[] tempFiles = tempFileDir.GetFiles();
        foreach (FileInfo tempFile in tempFiles)
        {
            if (tempFile.CreationTimeUtc < threadStartTime)
            {
                File.Delete(tempFile.FullName);
            }
        }
    }
    catch { }
});

} @Martijn Pieters This answer is a different solution that handles multiple questions. If anything, the other questions that I posted on, should be marked as duplicates because they are the same question. I posted on each of them to ensure that whoever has this problem, can find the solution.

Post a Comment for "Delete File After Sent To Mail As Intent.action_send"