Android的份额文件:我知道什么时候该文件已被共享或选择器被取消?什么时候、已被、该文件、份额

2023-09-06 10:51:46 作者:白日梦想家

我有一个共享选项的应用程序。当它在用户点击时,文件创建,然后一个选择器显示要求用户进行共享他怎么想分享(邮件,Skype和谷歌驱动器...)。一旦文件被共享或选配取消我要删除的文件,因为它不再需要。

I have an application with a Share option. When the user clicks on it, a file to be shared is created and then a chooser is shown to ask the user how does he want to share it (mail, skype, google drive...). Once the file has been shared or the chooser is cancelled I want to delete the file since it's no longer needed.

我想要的文件名具有当前的日期,因此,覆盖在同一文件中的每个时间不是一个选项。有没有什么办法知道什么时候是不是需要这个文件了?

I want the filename to have the current date, so overriding the same file each time is not an option. Is there any way to know when this file isn't needed anymore??

要分享我用这个code中的文件:

To share the file I use this code:

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/csv");
sharingIntent.putExtra(Intent.EXTRA_STREAM, csv);
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
startActivity(Intent.createChooser(sharingIntent, getResources().getText(R.string.send_to)));

谢谢!

推荐答案

而不是 startActivity ,选择器需要使用启动 startActivityForResult 。或选配没有关闭,直到它的取消文件是共享的,因此该文件可以在的onActivityResult 被删除。

Instead of startActivity, the chooser needs to be started using startActivityForResult. The chooser isn't closed until it's cancelled or the file is shared, so the file can be deleted in onActivityResult.