Android的6:不能共享文件了吗?文件、Android

2023-09-06 14:32:35 作者:无处安放的痛

我共享图像,这code正常工作的设备Android 6前:

I am sharing an image, and this code works properly for devices before Android 6:

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
Uri uri = Uri.fromFile(new File(mFilename));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
mContext.startActivity(Intent.createChooser(shareIntent, mChooserTitle));

不过,我得到了祝酒错误不能附加空文件当我尝试采用Android 6,共享

However I get the toast error "can't attach empty files" when I try to share using Android 6.

我证实该文件存在,它不是零长度。

I verified that the file exists and it's not zero-length.

任何人有一个解决方案吗?

Anyone has a solution for this?

推荐答案

在Android 6.0运行时许可制度的一个限制是,将会有其他的情况引起的问题。什么,你遇到的是:试图共享外部存储文件到一个应用程序没有运行权限检查到位特定的UI路径

One limitation of the Android 6.0 runtime permission system is that there will be corner cases that cause problems. What you encountered is one: trying to share a file on external storage to an app that does not have the runtime permission checks in place for that particular UI path.

我说这是一个角落案例,因为,在接收应用程序这个bug影响的用户,该用户就不能拥有previously使用的应用程序,并授予必要的权限。或者:

I say that this is a "corner case" because, for this bug in the receiving app to affect the user, the user cannot have previously used that app and granted the necessary permission. Either:

该用户从未使用过该应用程序之前,但它仍然在试图分享的内容,或者

The user has never used that app before, yet it still trying to share content to it, or

用户撤消通过设置权限,但并没有意识到,这将打破这种功能位

The user revoked the permission via Settings, but did not realize that it would break this bit of functionality

。这都是小概率事件。

您,作为发件人,主要有两个选择:

You, as the sender, have two main options:

使用文件中切换出来:// 乌里值,有利于一个文件服务的的ContentProvider FileProvider ,所以允许不再需要,或

Switch away from using file:// Uri values, in favor of a file-serving ContentProvider like FileProvider, so the permission is no longer needed, or

只是生活的角落情况下