简体   繁体   中英

How to access External Micro SD card of the phone?

Does anyone know how can I get SD card of the phone?

I know that someone will tell me its getExternalStorageDirectory() or Environment.getExternalStoragePublicDirectory() .

But unfortunately it doesn't always point to the external SD card in all the models. For example I tried in one model of samsung it works fine but another not, LG not. And also according to the documentation also its not always external SD card.

Here it is,

*"don't be confused by the word "external" here.

This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions).

Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer."*

In my application I want user to use SD card only.

How can I overcome with this?

I was facing the same problem. I didn't know how to access external sd card location. I was developing an app wherein I had to access the external sd card to read and write some stuff. I tried different methods including the pre-defined android libraries. These are the methods I used:-

These were the misses:-

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File myFile = new File(path, "test.txt"); 

File root = Environment.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath()+"/Download");

File f = getExternalFilesDir(null); 
File file = new File(f,"test.txt");

These all were accessing internal storage "/storage/sdcard0" or "/storage/emulated/0". The reason being in the android device the portion of the internal storage acts as an external storage. So in case you have internal storage of around 16 Gb or more and there is an option to expand the device with sd card, then this is the only way i guess to access the external sd card because even the built-in functions and libraries of the android studio will access internal storage as external storage.

Finally I used this:-

String extFilePath = "/storage/sdcard1/Download";
File myFile = new File(extFilePath, "test.txt");

and it worked. So you see where pre-defined android libraries/functions fail, I was able to do the task with the simple String.

Apart from this if you want to check the path for external storage your device, try this:-

String sdpath,sd1path,usbdiskpath,sd0path;

if(new File("/storage/extSdCard/").exists())
{sdpath="/storage/extSdCard/";
Log.i("Sd Cardext Path", sdpath);}

if(new File("/storage/sdcard1/").exists())
{sd1path="/storage/sdcard1/";
Log.i("Sd Card1 Path",sd1path);}

if(new File("/storage/usbcard1/").exists())
{usbdiskpath="/storage/usbcard1/";
Log.i("USB Path",usbdiskpath);}

if(new File("/storage/sdcard0/").exists())
{sd0path="/storage/sdcard0/";
Log.i("Sd Card0 Path",sd0path);}

Checking these might help you know what path to choose while accessing external sd card. I hope this helps others.

Check out the answer by CommonsWare on the same topic https://stackoverflow.com/a/5695129/582571

He mention that we can not distinguish between mobile's inbuilt external storage and removable external storage.

But by Aleadam answer https://stackoverflow.com/a/6049446/582571 we can only check that is the External Storage is removable or not with isExternalStorageRemovable() function.

I hope you will get idea.

You can below condition to check whether SDCard is available or not

if (android.os.Environment.getExternalStorageState().equals(
            android.os.Environment.MEDIA_MOUNTED)) {

     //Check for the file
     File appFolder = new File(Environment.getExternalStorageDirectory() + File.separator
                + context.getString(R.string.app_name));

     boolean exist = appFolder.exists();
}

Removable storage path is diff in device to device.
The example of Removable path are:
1. mnt/ext
2. mnt/externalsd
3. mnt/external_sd


My device use mnt/external_sd.

you can check the path of your file from vold.fstab file.
this file is under systems folder.
The path of file is:

"/etc/vold.fstab"

I was facing the same problem with my latest device. Then I found that removable SD card can be accessed at /mnt/ext_sdcard/. and it worked for me. I was able to list all the files stored in removable external sd card at this location.

Following is the code: new File("/mnt/ext_sdcard/").listFiles();

You can get the path like the following way.....

File sdCardRoot = Environment.getExternalStorageDirectory();
        PATH = sdCardRoot.toString();

If the path not exist, then you have to make the path by mkdir().....

private File getTempFile(Context context) {
        final File path = new File(Environment.getExternalStorageDirectory(),
                context.getPackageName());
        if (!path.exists()) {
            path.mkdir();
        }
        return new File(path, "new.png");
    }

The above function will return the file...

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