External Sdcard File Path For Android
Solution 1:
Is it true that the file path to external SDCard on Android devices are always "/storage/extSdCard"? If not, how many variations are there?
Sadly the path to the external storage is not always the same according to manufacturer. Using Environment.getExternalStorageDirectory()
will return you the normal path for SD card which is mnt/sdcard/
. But for Samsung devices for example, the SD card path is either under mnt/extSdCard/
or under mnt/external_sd/
.
So one way to proceed would be to check the existence of external directory according to the path used by each manufacturer. With something like this:
mExternalDirectory = Environment.getExternalStorageDirectory()
.getAbsolutePath();
if (android.os.Build.DEVICE.contains("samsung")
|| android.os.Build.MANUFACTURER.contains("samsung")) {
Filef=newFile(Environment.getExternalStorageDirectory()
.getParent() + "/extSdCard" + "/myDirectory");
if (f.exists() && f.isDirectory()) {
mExternalDirectory = Environment.getExternalStorageDirectory()
.getParent() + "/extSdCard";
} else {
f = newFile(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/external_sd" + "/myDirectory");
if (f.exists() && f.isDirectory()) {
mExternalDirectory = Environment
.getExternalStorageDirectory().getAbsolutePath()
+ "/external_sd";
}
}
}
But what I really want is detect whether physical SDCard is mounted or not.
I didn't try the code yet, but the approach of Dmitriy Lozenko in this answer is much more interesting. His method returns the path of all mounted SD cards on sytem regardless of the manufacturer.
Solution 2:
This is how I finally got sdcard path using :
publicStringgetExternalStoragePath() {
String internalPath = Environment.getExternalStorageDirectory().getAbsolutePath();
String[] paths = internalPath.split("/");
String parentPath = "/";
for (String s : paths) {
if (s.trim().length() > 0) {
parentPath = parentPath.concat(s);
break;
}
}
File parent = newFile(parentPath);
if (parent.exists()) {
File[] files = parent.listFiles();
for (File file : files) {
String filePath = file.getAbsolutePath();
Log.d(TAG, filePath);
if (filePath.equals(internalPath)) {
continue;
} elseif (filePath.toLowerCase().contains("sdcard")) {
return filePath;
} elseif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
try {
if (Environment.isExternalStorageRemovable(file)) {
return filePath;
}
} catch (RuntimeException e) {
Log.e(TAG, "RuntimeException: " + e);
}
}
}
}
returnnull;
}
Solution 3:
I hope it will be useful for you :)
import android.os.Environment;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
publicclassMemoryStorage {
privateMemoryStorage() {}
publicstaticfinalStringSD_CARD="sdCard";
publicstaticfinalStringEXTERNAL_SD_CARD="externalSdCard";
/**
* @return True if the external storage is available. False otherwise.
*/publicstaticbooleanisAvailable() {
Stringstate= Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
returntrue;
}
returnfalse;
}
publicstatic String getSdCardPath() {
return Environment.getExternalStorageDirectory().getPath() + "/";
}
/**
* @return True if the external storage is writable. False otherwise.
*/publicstaticbooleanisWritable() {
Stringstate= Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
returntrue;
}
returnfalse;
}
/**
* @return A map of all storage locations available
*/publicstatic Map<String, File> getAllStorageLocations() {
Map<String, File> map = newHashMap<String, File>(10);
List<String> mMounts = newArrayList<String>(10);
List<String> mVold = newArrayList<String>(10);
mMounts.add("/mnt/sdcard");
mVold.add("/mnt/sdcard");
try {
FilemountFile=newFile("/proc/mounts");
if (mountFile.exists()) {
Scannerscanner=newScanner(mountFile);
while (scanner.hasNext()) {
Stringline= scanner.nextLine();
if (line.startsWith("/dev/block/vold/")) {
String[] lineElements = line.split(" ");
Stringelement= lineElements[1];
// don't add the default mount path// it's already in the list.if (!element.equals("/mnt/sdcard"))
mMounts.add(element);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
try {
FilevoldFile=newFile("/system/etc/vold.fstab");
if (voldFile.exists()) {
Scannerscanner=newScanner(voldFile);
while (scanner.hasNext()) {
Stringline= scanner.nextLine();
if (line.startsWith("dev_mount")) {
String[] lineElements = line.split(" ");
Stringelement= lineElements[2];
if (element.contains(":"))
element = element.substring(0, element.indexOf(":"));
if (!element.equals("/mnt/sdcard"))
mVold.add(element);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
for (inti=0; i < mMounts.size(); i++) {
Stringmount= mMounts.get(i);
if (!mVold.contains(mount))
mMounts.remove(i--);
}
mVold.clear();
List<String> mountHash = newArrayList<String>(10);
for (String mount : mMounts) {
Fileroot=newFile(mount);
if (root.exists() && root.isDirectory() && root.canWrite()) {
File[] list = root.listFiles();
Stringhash="[";
if (list != null) {
for (File f : list) {
hash += f.getName().hashCode() + ":" + f.length() + ", ";
}
}
hash += "]";
if (!mountHash.contains(hash)) {
Stringkey= SD_CARD + "_" + map.size();
if (map.size() == 0) {
key = SD_CARD;
} elseif (map.size() == 1) {
key = EXTERNAL_SD_CARD;
}
mountHash.add(hash);
map.put(key, root);
}
}
}
mMounts.clear();
if (map.isEmpty()) {
map.put(SD_CARD, Environment.getExternalStorageDirectory());
}
return map;
}
}
Solution 4:
I just figured out something. At least for my Android Emulator, I had the SD Card Path like ' /storage/????-???? ' where every ? is a capital letter or a digit.
So, if /storage/ directory has a directory which is readable and that is not the internal storage directory, it must be the SD Card.
My code worked on my android emulator!
String removableStoragePath;
File fileList[] = newFile("/storage/").listFiles();
for (File file : fileList)
{ if(!file.getAbsolutePath().equalsIgnoreCase(Environment.getExternalStorageDirectory().getAbsolutePath()) && file.isDirectory() && file.canRead())
removableStoragePath = file.getAbsolutePath(); }
//If there is an SD Card, removableStoragePath will have it's path. If there isn't it will be an empty string.
If there is an SD Card, removableStoragePath will have it's path. If there isn't it will be an empty string.
Solution 5:
I have got solution on this after 4 days, Please note following points while giving path to File class in Android(Java):
- Use path for internal storage String
path="/storage/sdcard0/myfile.txt";
- use path for external storage
path="/storage/sdcard1/myfile.txt";
- mention permissions in Manifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
- First check file length for confirmation.
- Check paths in ES File Explorer regarding sdcard0 & sdcard1 is this same or else...
e.g.:
Filefile=newFile(path);
long = file.length();//in Bytes
Post a Comment for "External Sdcard File Path For Android"