Does anyone know how can I get SD card of the phone?
I know that someone will tell me its getExternalStorageDirectory()
or Environment.getExterna
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:
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 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();
}
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.
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...
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.