System.getenv(“SECONDARY_STORAGE”) return null on

2019-08-13 18:18发布

I'm trying to get a List of external storages on android devices and in some devices the instruction System.getenv("SECONDARY_STORAGE"); returns null although the sd card is mounted in the device (4.1.1).

I'm using the Dimitriy solution to do this and works fine but in some devices I'm having this problem.

Someone know in what cases System.getenv("SECONDARY_STORAGE") can return null?

Thanks a lot

2条回答
淡お忘
2楼-- · 2019-08-13 18:59

In my experiments, Some old HTC devices will returns null when you call System.getenv("SECONDARY_STORAGE")

I found that the actual microSD path in such devices is be mounted in

/storage/sdcard0/ext_sd/

/sdcard2/

/mnt/sdcard/ext_sd/

Due to external storage environment variable is defined by phone manufacturers. We don't have a API to exactly get the microSD path.

查看更多
劫难
3楼-- · 2019-08-13 19:02

Here is the solution. try it...

First get All files in list

var file=new Java.IO.File("storage/");
var listOfStorages=file.ListFiles();

var isSDPresent=false;
if(listOfStorages[1].Name.Containes("emulated")||listOfStorages[0].Name.Containes("-"))
{
   isSDPresent=true;
}
查看更多
登录 后发表回答