英文:
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:
<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"/>
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(<path_to_File>);
FileInputStream fileInputStream = new FileInputStream(file); // Here it will through the exception
For other type of files it works just fine.
Any solution for that?
<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"/>
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论