gpt4 book ai didi

android - FileProvider.getUriForFile vs Uri.fromFile - 除了权限之外还有其他区别吗

转载 作者:搜寻专家 更新时间:2023-11-01 08:28:14 24 4
gpt4 key购买 nike

在我的 Andoroid 应用程序中,我想让用户使用用户喜欢的任何发送方法(例如电子邮件、Skype、Viber、蓝牙等)发送我的应用程序生成的文件我正在使用 Intent.ACTION_SEND 如下:

File readF = new File(fullFileName);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.some_subject));
intent.putExtra(Intent.EXTRA_TEXT, getResources().getString(R.string.some_message_body));
Uri uri = FileProvider.getUriForFile(this, "my.package.fileprovider", readF);
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, getResources().getString(R.string.some_info_message));

正如预期的那样,它成功地通过 Gmail 和蓝牙发送了文件。

但是当用户选择Skype时,文件被发送了,但是它的名字被改变了

当用户选择 Viber 时,不会发送任何内容。

如果我只改变构建 Uri 的方式:

Uri uri = Uri.fromFile(new File("/some/publicly/available/file"));

然后文件通过 Skype 和 Viber 成功发送,名称被保留。

那么在 Intent 中使用 FileProvider 与直接使用公共(public)文件有什么区别。

最佳答案

I am using Intent.ACTION_SEND as follows:

我建议您将 Intent.FLAG_GRANT_READ_URI_PERMISSION 添加到该 Intent

Then the file is successfully sent both over Skype, and Viber, and the name is preserved.

直到您的 targetSdkVersion 攀升至 24 或更高,您才开始在 Android 7.0+ 上运行您的应用程序。然后,您的应用程序崩溃并出现 FileUriExposedException

Then the file is successfully sent both over Skype, and Viber, and the name is preserved.

现在,也许。这些应用程序的作者和任何其他人没有义务维护您的文件名。他们可以为所欲为,因为这是他们的应用程序,而不是您的。

So what is the difference in using FileProvider vs directly using a public file in Intent.

FileProvider 大致类似于 Web 服务器。它允许一个应用程序与其他应用程序共享内容,而这些应用程序不必直接访问底层文件。 Google 正试图避免让外部存储成为随机垃圾的垃圾场,以支持诸如 ACTION_SEND 之类的东西,因此在 Android 7.0+ 上设置了 StrictMode“如果您尝试传递 file Uri(例如,Uri.fromFile()),开箱即用”会使应用程序崩溃。

关于android - FileProvider.getUriForFile vs Uri.fromFile - 除了权限之外还有其他区别吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42796774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com