首页 > 解决方案 > 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" />

标签: javaandroidfileexceptionpermissions

解决方案


File对象将通过以下方式创建:

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "asd.txt");

而且,正如 Igor 指出的那样,您需要为READ_EXTERNAL_STORAGE.


推荐阅读