Delete file after sharing via intent

前端 未结 4 884
轮回少年
轮回少年 2021-01-17 12:13

I\'m trying to delete a temporary file after sharing it via android\'s Intent.ACTION_SEND feature. Right now I am starting the activity for a result and in OnActivityResult,

4条回答
  •  没有蜡笔的小新
    2021-01-17 12:39

    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. 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.

提交回复
热议问题