java - How to access file in documents folder?
问题描述
Im trying to acces a file from the documents folder, but it's always throws an exception.. What is the correct code for that path? Im using a samsung SM-G950F Android 8.0.0
File file = new File(Environment.getExternalStorageDirectory() + "/sdcard/Documents/asd.txt");
java.io.FileNotFoundException: /storage/emulated/0/sdcard/Documents/asd.txt (No such file or directory)
File file = new File("/sdcard/Documents/asd.txt");
java.io.FileNotFoundException: /sdcard/Documents/asd.txt (Permission denied)
Im getting a permission denied here and i have these permissions in my manifest file:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
解决方案
该File
对象将通过以下方式创建:
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "asd.txt");
而且,正如 Igor 指出的那样,您需要为READ_EXTERNAL_STORAGE
.
推荐阅读
- kubernetes - Kubernetes 从 Cronjob 创建作业不起作用
- c++ - 将 .wav 加载到内存中,然后在 C++ 中播放
- flutter - 迁移到 Apple M1 后,我的应用程序不再启动
- python - 向用户 discord.py 的 DM 发送消息时出错
- javascript - 重新渲染后 React Refs 为空
- single-sign-on - Laravel 临时 SSO 方法
- java - Oracle 19c JDBC(即时客户端 basiclite)驱动程序问题
- javascript - 找不到此 switch 语句的问题所在,它在我的 VS 代码编辑器中的 case 语法中始终显示红色下划线
- angular - 在 BsModalRef 中模拟 modal.content.onClose.subscribe
- python - 在django中调用一对一模型时如何调用应用程序名称?