Getting the path to SD card

冷暖自知 提交于 2020-01-02 11:06:46

问题


Please read the whole post before down-voting and/or marking it as a duplicate!

I'm working on an app that reads files from within a specific folder on the user's phone - either from the SD card (if there's one) or from the built in storage. Yes, the "READ_EXTERNAL_STORAGE" is mentioned in the manifest and I'm also handling the permission popup for API>23.

I used to simply use

File folder = new File(Environment.getExternalStorageDirectory(), "myfolder");

to get the path of the folder that is stored in the built in storage (32gb for an S7) but now I want to get the path to the SD card. According to pretty much every result google gave me, "Environment.getExternalStorageDirectory()" is supposed to give you the path to the SD card but for me it doesn't (and never has).

I've tested the following with two different Samsung Galaxy S7s, both with Android 7.0, one with an SD card (+ the folder), the other without (+ the folder):

Log.d(tag, System.getenv("EXTERNAL_STORAGE"));
Log.d(tag, System.getenv("SECONDARY_STORAGE"));
Log.d(tag, ""+new File(System.getenv("EXTERNAL_STORAGE")+File.separator+"myfolder").isDirectory());
Log.e(tag, ""+new File(System.getenv("EXTERNAL_STORAGE")+File.separator+ordner).getAbsolutePath());
Log.d(tag, Environment.isExternalStorageRemovable());
Log.d(tag, Environment.getExternalStorageDirectory());
Log.d(tag, Environment.getExternalStorageDirectory().getAbsolutePath());

To my surprise both phones output the same infos:

/sdcard
null
true
/sdcard/myfolder
false
/storage/emulated/0
/storage/emulated/0

According to the file manager app ("My Files"), the built in storage is called "Internal Storage", which makes even less sense (I know the difference between Internal and External Storage in Android).

How do I get the path to the actual SD card (without hardcoding it)?


回答1:


The only way I found is to semi-hardcode it:

File[] folders = myappcontext.getExternalCacheDirs();

gives you the path to the "cache" folders your app has access to (but that are deleted when you uninstall your app).

If the phone uses a removable SD card (that is currently mounted), the length of the array should be "2":

  1. The path to the "cache" folder in the external (not removable) storage
  2. The path to the "cache" folder on your SD card

They look something like this:

/storage/emulated/0/Android/data/com.mycompany.myapp/cache
/storage/xxxx-xxxx/Android/data/com.mycompany.myapp/cache

... where "x" is the number (id?) of your sd card. I've only been able to test it with 2 different SD cards and both had their own number.

Environment.getExternalStorageDirectory();

should also give you

/storage/emulated/0/

which is the non-hardcoding way of getting access to the external storage. ;)

If you create a new folder on the very first level of your SD card on your PC, its path will be:

/storage/xxxx-xxxx/myfolder

I also have to warn you: While you can read the "myfolder" folder, you can't write in it (will just throw an "Access Denied" exception with Android 7) because of the changes to the whole system that came with Kitkat. But that's a different problem I'm going to address in a new question.




回答2:


you have to insert the following permission into your application's manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android" >

   <!-- Add this -->
   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

</manifest>


来源:https://stackoverflow.com/questions/50132764/getting-the-path-to-sd-card

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!