简体   繁体   中英

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" />

从Android 6.0开始,您必须请求权限https://developer.android.com/guide/topics/permissions/overview

The File object would be created via:

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

And, as Igor points out , you will need to request runtime permissions for READ_EXTERNAL_STORAGE .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM