Delete file after sharing with intent

I am trying to delete a temporary file after using it through the Android function Intent.ACTION_SEND. Right now I am running an action for the result and in OnActivityResult, I am deleting the file. Unfortunately, this only works if I debug it with a breakpoint, but when I let it run and talk freely, write a file, the letter has no attachment.

I think what happens is that my activity is to delete the file before sending it by email. What I am not getting is why, should I not call onActivityResult only AFTER the completion of another action?

I also tried deleting the file in onResume, but no luck.

Is there a better way to do this?

+4
source share
4 answers

I managed to get it to work with:

File tbd = new File(sharePath); tbd.deleteOnExit(); 

It seems that the file is being deleted when the action closes.

-1
source

I noticed the same behavior with a similar approach. While looking at logcat for errors, I saw that gmail complained that it could not find the attachment. So yes, it seems that the target returns before gmail actually read the file for attachment.

I have not found a solution yet, but most likely it will look like:

  • move the file to some directory so that I know it, I decided to send
  • send it as an app via ACTION_SEND
  • in the next onResume for my activity on the start screen, delete the files in the "sent" directory that are older than some time frames that are reasonable enough to actually send.

Choosing the right time interval can be difficult, since this is probably the case when gmail (or other ACTION_SEND providers) do not actually read the file until it receives a network connection. I think that 24 hours should be reasonable, and in my case I am dealing with diagnostic logs, so there is no real harm when deleting too early if the user is disconnected from the network for a long period of time.

If the contents of your file are textual and not indecently large, a simpler approach might be to read the contents of the file and use Intent.putExtra (android.content.Intent.EXTRA_TEXT, yourText) to insert it into the body of the message.

+4
source

I did the following.

I used:

 myfile.deleteOnExit(); 

However, as DR the correct answer mentioned in the comment below does not guarantee file deletion. That is why I also delete the file after the Shared Activity returns. I delete the file if the file exists. Since the application sometimes crashes, I put it inside try{} and it works.

I don’t know why this does not work for you, but for me it works, at least for attaching Gmail, TextSecure, Hangouts.

In the delcaration class:

 static File file; 

In the method that causes the intent:

  Intent share = new Intent(Intent.ACTION_SEND); share.setType("image/png"); // Compress the bitmap to PNG ByteArrayOutputStream bytes = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes); // Temporarily store the image to Flash File sdCard = Environment.getExternalStorageDirectory(); File dir = new File (sdCard.getAbsolutePath() + "/FolderName"); dir.mkdirs(); // This file is static. file = new File(dir, "FileName.png"); try { file.createNewFile(); FileOutputStream fo = new FileOutputStream(file); fo.write(bytes.toByteArray()); fo.flush(); fo.close(); } catch (IOException e) { e.printStackTrace(); } // Share compressed image share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///"+file.getPath())); /** START ACTIVITY **/ startActivityForResult(Intent.createChooser(share,"Share Image"),1); // Delete Temporary file file.deleteOnExit(); // sometimes works 

In an additional method

 protected void onActivityResult(int requestCode, int resultCode, Intent data) { // Because app crashes sometimes without the try->catch try { // if file exists in memory if (file.exists()) { file.delete(); } } catch (Exception e) { Log.d(LOG,"Some error happened?"); } } 
+2
source

Another potential answer would be to create a new stream when your application resumes, immediately note the current time, the sleeping stream, how much would you consider reasonable to send a file, and when the stream resumes, only files created before the previously marked time are deleted. This will give you the ability to delete only what was in the repository at the time your application was resumed, but also gives gmail time to receive email. Code snippet: (I am using C # / Xamarin, but you should get an 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 another solution that handles several questions. Anyway, the other questions that I posted should be marked as duplicates, because this is the same question. I placed on each of them to make sure that anyone who has this problem can find a solution.

0
source

Source: https://habr.com/ru/post/1334425/


All Articles