Reputation: 3054
I have following code :
Uri screenshotUri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
intent.putExtra(Intent.EXTRA_SUBJECT, "Location of " + name);
intent.putExtra(Intent.EXTRA_TITLE, getText(R.string.screen_share_message));
intent.putExtra(Intent.EXTRA_TEXT, getText(R.string.screen_share_message));
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Share with"));
After user sends or share the file, How to delete it?
Upvotes: 2
Views: 3017
Reputation: 878
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)
public static void ClearTempFiles()
{
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.
Upvotes: 0
Reputation: 3623
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.
protected void onResume() {
// TODO Auto-generated method stub
File file= new File(android.os.Environment.getExternalStorageDirectory().toString()+ "/akanksha" + ".png");
if(file.exists())
{
file.delete();
}
super.onResume();
}
Upvotes: 0
Reputation: 48602
First Refer @Dheeraj V.S. answer.
Ways to delete the files
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.
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
public void deleteFromExternalStorage(String fileName) {
String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/directoryname";
try
{
File file = new File(fullPath, fileName);
if(file.exists())
file.delete();
}
catch (Exception e)
{
Log.e("APP", "Exception while deleting file " + e.getMessage());
}
}
Upvotes: 1
Reputation: 28737
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
Upvotes: 2