Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 558
hiren soni Avatar asked Jul 30 '12 06:07

hiren soni


People also ask

How do I access my Micro SD card on my phone?

Head to Settings > Storage & USB, and you'll see the microSD cards you have installed. If your microSD card is set up as portable storage and you want to switch over to internal storage, select the drive, then tap the menu button in the top right of the screen.

How do I access my external SD card on my Android phone?

To read SD card content on Android devices, you can utilize an external SD card reader for Android. You can just use the SD card reader to connect the SD card, microSD card, memory card to your Android phone or tablet. Then read and transfer files between your Android device and external SD memory.


4 Answers

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.

like image 184
rajpara Avatar answered Oct 05 '22 20:10

rajpara


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();
}
like image 36
Nirali Avatar answered Oct 05 '22 20:10

Nirali


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.

like image 44
Manmeet Khurana Avatar answered Oct 05 '22 19:10

Manmeet Khurana


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"
like image 22
AmmY Avatar answered Oct 05 '22 18:10

AmmY