简体   繁体   中英

Accessing the SD-Card in an Android Application

I am currently developing an Android Application, that will open and save .txt files. In order to make it easy to read external .txt files i want to make sure, that they are saved in the app's folder on the SD-Card. Unfortunately i am only able to access /storage/emulated/0.

I already tried 2 different possibilites:

Environment.getExternalStorageDirectory()
getExternalFilesDir("MyFileStorage")

They both lead to /storage/emulated/0 on the internal storage. Does anyone know how i can access the app's folder on the SD Card of my Smartphone (USB-Debugging).

Apps don't come with a folder on the external storage by default. You will need to create a folder for your app like this:

String folder_main = "YourAppName";

File f = new File(Environment.getExternalStorageDirectory(), folder_main);
if (!f.exists()) {
    f.mkdirs();
}

Remember that you need to add the permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

to your AndroidManifest.xml to access the SD card (and use the permissions model introduced in Android 6.0 for newer devices).

that they are saved in the app's folder on the SD-Card

By default, apps do not have a directory on removable storage .

On Android 4.4+, you are welcome to use getExternalFilesDirs() (note the s on the end). If this returns a list with 2+ entries, the first one will be on external storage, and the others will be on removable storage.

I already tried 2 different possibilites... They both lead to /storage/emulated/0 on the internal storage

Those point to what the Android SDK refers to as 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