Android 13 – 许可访问PDF或其他类型的文件

huangapple go评论65阅读模式
英文:

Android 13 - Permission to access PDF or other type of files

问题

按要求,针对 Android 13+ 的应用程序,我正在使用以下权限请求来访问媒体文件:

<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO"/>

我遇到的问题是,这些权限似乎不足以访问存储中保存的其他类型的文件,例如 PDF 文件、贴纸等。当我尝试访问这些文件时,会抛出 FileNotFoundException 异常,因为缺乏适当的权限。

例如:

File file = new File(<path_to_File>);
FileInputStream fileInputStream = new FileInputStream(file); // 在这里会抛出异常

对于其他类型的文件,它可以正常工作。

有解决方法吗?

英文:

As required, for apps targeting Android 13+ I'm using the following permission requests for the media files:

&lt;uses-permission android:name=&quot;android.permission.READ_MEDIA_IMAGES&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.READ_MEDIA_VIDEO&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.READ_MEDIA_AUDIO&quot;/&gt;

The problem that I have is that this permissions doesn't seem to be enough to access other type of files such as pdfs, stickers etc that are saved in the storage.
When I want to access such files it will through a FileNotFoundException due to lack of proper permissions.

For example:

File file = new File(&lt;path_to_File&gt;);
FileInputStream fileInputStream = new FileInputStream(file); // Here it will through the exception

For other type of files it works just fine.

Any solution for that?

&lt;uses-permission android:name=&quot;android.permission.READ_MEDIA_IMAGES&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.READ_MEDIA_VIDEO&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.READ_MEDIA_AUDIO&quot;/&gt;

答案1

得分: 2

解决方案是使用SAF,例如使用ACTION_OPEN_DOCUMENT来让用户选择文件。

完全不需要权限。

英文:

The solution is to use SAF and for exemple ACTION_OPEN_DOCUMENT to let the user pick the file.

No permissions needed at all.

huangapple
  • 本文由 发表于 2023年6月29日 21:46:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581641.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定