Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
630 views
in Technique[技术] by (71.8m points)

file - How to get the internal and external sdcard path in android

Most of the new android devices have an internal sdcard and an external sdcard. I want to make a file explorer app but I can't find out how to get the path to use in my app because

File file = Environment.getExternalStorageDirectory();

just returns in most device /mnt/sdcard but there is another path for the other external sdcard like /storage1 or /storage2 . Any help appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How to get the internal and external sdcard path in android

Methods to store in Internal Storage:

File getDir (String name, int mode)

File getFilesDir () 

Above methods are present in Context class

Methods to store in phone's internal memory:

File getExternalStorageDirectory ()

File getExternalFilesDir (String type)

File getExternalStoragePublicDirectory (String type)

In the beginning, everyone used Environment.getExternalStorageDirectory() , which pointed to the root of phone's internal memory. As a result, root directory was filled with random content.

Later, these two methods were added:

In Context class they added getExternalFilesDir(), pointing to an app-specific directory on phone's internal memory. This directory and its contents will be deleted when the app is uninstalled.

Environment.getExternalStoragePublicDirectory() for centralized places to store well-known file types, like photos and movies. This directory and its contents will NOT be deleted when the app is uninstalled.

Methods to store in Removable Storage i.e. micro SD card

Before API level 19, there was no official way to store in SD card. But many could do it using unofficial APIs.

Officially, one method was introduced in Context class in API level 19 (Android version 4.4 - Kitkat).

File[] getExternalFilesDirs (String type)

It returns absolute paths to application-specific directories on all shared/external storage devices where the application can place persistent files it owns. These files are internal to the application, and not typically visible to the user as media.

That means, it will return paths to both Micro SD card and Internal memory. Generally, second returned path would be storage path of micro SD card.

The Internal and External Storage terminology according to Google/official Android docs is quite different from what we think.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...