Android - Api 23 Unable To Get Usb/sd Card Directory
I used to get all the files in USB/SD Card from '/storage/external_storage' like so File = new File('/storage/external_storage'); file.listFiles() and show the files and folders i
Solution 1:
It is true the doc is not very clear about that....but it seems you have to ask the system to mount the media device for you. Can you try to use
Environment.getExternalStorageState()
to see what it returns you?
Otherwise, you can also try to access the SD/External USB by using this class:
publicclassExternalStorage {
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;
}
}
The use is like this:
Map<String, File> externalLocations = ExternalStorage.getAllStorageLocations();
File sdCard = externalLocations.get(ExternalStorage.SD_CARD);
File externalSdCard = externalLocations.get(ExternalStorage.EXTERNAL_SD_CARD);
Source here: Find an external SD card location
Solution 2:
Have a look at getExternalFilesDirs()
.
If a micro SD card is inserted the second entry will point to the card.
If you then attach a USB OTG drive the third entry will point to the drive.
With Intent.ACTION_OPEN_DOCUMENT_TREE you can let the user choose from all drives and then use Storage Access Framework.
Post a Comment for "Android - Api 23 Unable To Get Usb/sd Card Directory"