Created
April 29, 2014 15:09
-
-
Save paour/11403182 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.frogsparks.mytrails.util; | |
import android.os.Environment; | |
import com.frogsparks.mytrails.MyTrails; | |
import java.io.File; | |
import java.util.*; | |
// http://stackoverflow.com/a/15612964/304876 | |
public class ExternalStorage { | |
public static final String TAG = "ExternalStorage: "; | |
public static final String SD_CARD = "sdCard"; | |
public static final String EXTERNAL_SD_CARD = "externalSdCard"; | |
/** | |
* @return True if the external storage is available. False otherwise. | |
*/ | |
public static boolean isAvailable() { | |
String state = Environment.getExternalStorageState(); | |
if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { | |
return true; | |
} | |
return false; | |
} | |
public static String getSdCardPath() { | |
return Environment.getExternalStorageDirectory().getPath() + "/"; | |
} | |
/** | |
* @return True if the external storage is writable. False otherwise. | |
*/ | |
public static boolean isWritable() { | |
String state = Environment.getExternalStorageState(); | |
if (Environment.MEDIA_MOUNTED.equals(state)) { | |
return true; | |
} | |
return false; | |
} | |
/** | |
* @return A map of all storage locations available | |
*/ | |
public static Map<String, File> getAllStorageLocations() { | |
Map<String, File> map = new HashMap<String, File>(10); | |
List<String> mMounts = new ArrayList<String>(10); | |
List<String> mVold = new ArrayList<String>(10); | |
mMounts.add("/mnt/sdcard"); | |
mVold.add("/mnt/sdcard"); | |
try { | |
File mountFile = new File("/proc/mounts"); | |
if (mountFile.exists()) { | |
Scanner scanner = new Scanner(mountFile); | |
while (scanner.hasNext()) { | |
String line = scanner.nextLine(); | |
Log1.d(MyTrails.APPTAG, TAG + "getAllStorageLocations /proc/mounts " + line); | |
if (line.startsWith("/dev/block/vold/")) { | |
String[] lineElements = line.split(" "); | |
String element = lineElements[1]; | |
// don't add the default mount path | |
// it's already in the list. | |
if (!element.equals("/mnt/sdcard")) | |
mMounts.add(element); | |
} | |
} | |
} else { | |
Log1.d(MyTrails.APPTAG, TAG + "getAllStorageLocations no /proc/mounts"); | |
} | |
} catch (Exception e) { | |
Log1.e(MyTrails.APPTAG, TAG + "getAllStorageLocations", e); | |
} | |
boolean voldExists; | |
try { | |
File voldFile = new File("/system/etc/vold.fstab"); | |
if (voldFile.exists()) { | |
voldExists = true; | |
Scanner scanner = new Scanner(voldFile); | |
while (scanner.hasNext()) { | |
String line = scanner.nextLine(); | |
Log1.d(MyTrails.APPTAG, TAG + "getAllStorageLocations vold.fstab " + line); | |
if (line.startsWith("dev_mount")) { | |
String[] lineElements = line.split(" "); | |
String element = lineElements[2]; | |
if (element.contains(":")) | |
element = element.substring(0, element.indexOf(":")); | |
if (!element.equals("/mnt/sdcard")) | |
mVold.add(element); | |
} | |
} | |
} else { | |
voldExists = false; | |
Log1.d(MyTrails.APPTAG, TAG + "getAllStorageLocations no /system/etc/vold.fstab"); | |
} | |
} catch (Exception e) { | |
voldExists = false; | |
Log1.e(MyTrails.APPTAG, TAG + "getAllStorageLocations", e); | |
} | |
if (voldExists) { | |
for (int i = 0; i < mMounts.size(); i++) { | |
String mount = mMounts.get(i); | |
if (!mVold.contains(mount)) | |
mMounts.remove(i--); | |
} | |
mVold.clear(); | |
} | |
List<String> mountHash = new ArrayList<String>(10); | |
for (String mount : mMounts) { | |
File root = new File(mount); | |
if (root.exists() && root.isDirectory() && root.canWrite()) { | |
File[] list = root.listFiles(); | |
String hash = "["; | |
if (list != null) { | |
for (File f : list) { | |
hash += f.getName().hashCode() + ":" + f.length() + ", "; | |
} | |
} | |
hash += "]"; | |
if (!mountHash.contains(hash)) { | |
String key = SD_CARD + "_" + map.size(); | |
if (map.size() == 0) { | |
key = SD_CARD; | |
} else if (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()); | |
} | |
Log1.d(MyTrails.APPTAG, TAG + "getAllStorageLocations " + map); | |
return map; | |
} | |
} |
I tried your method, but in my platform, the path could be /mnt/media_rw/udisk/sda4
or /storage/udisk/sda4
.But the question is when I use this /mnt/media_rw/udisk/sda4
, the root.exists()
return false, but when I use /storage/udisk/sda4
, root
return true.
Here is my /proc/mounts
:
rootfs / rootfs ro,relatime 0 0 devtmpfs /dev devtmpfs rw,seclabel,relatime,size=111156k,nr_inodes=22811,mode=755 0 0 tmpfs /dev tmpfs rw,seclabel,nosuid,relatime,mode=755 0 0 devpts /dev/pts devpts rw,seclabel,relatime,mode=600,ptmxmode=000 0 0 proc /proc proc rw,relatime 0 0 sysfs /sys sysfs rw,seclabel,relatime 0 0 selinuxfs /sys/fs/selinux selinuxfs rw,relatime 0 0 debugfs /sys/kernel/debug debugfs rw,relatime 0 0 none /mnt/media_rw tmpfs rw,seclabel,relatime,mode=700,uid=1023,gid=1023 0 0 none /storage tmpfs rw,seclabel,relatime,mode=751,uid=1028,gid=1028 0 0 tmpfs /mnt/secure tmpfs rw,seclabel,relatime,mode=700 0 0 tmpfs /tmp tmpfs rw,seclabel,relatime,size=20480k,mode=771 0 0 tmpfs /mnt/asec tmpfs rw,seclabel,relatime,mode=755,gid=1000 0 0 tmpfs /mnt/obb tmpfs rw,seclabel,relatime,mode=755,gid=1000 0 0 /dev/block/mmcblk0p1 /system ext4 rw,seclabel,relatime,data=ordered 0 0 /dev/block/mmcblk0p2 /data ext4 rw,seclabel,nosuid,nodev,noatime,nomblk_io_submit,errors=panic,data=ordered 0 0 /dev/block/mmcblk0p3 /cache ext4 rw,seclabel,nosuid,nodev,noatime,nomblk_io_submit,errors=panic,data=ordered 0 0 /dev/fuse /mnt/shell/emulated fuse rw,nosuid,nodev,relatime,user_id=1023,group_id=1023,default_permissions,allow_other 0 0 /dev/block/vold/8:4 /mnt/media_rw/udisk/sda4 vfat rw,dirsync,nosuid,nodev,noexec,relatime,uid=1023,gid=1023,fmask=0007,dmask=0007,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,usefree,utf8,errors=remount-ro 0 0 /dev/fuse /storage/udisk/sda4 fuse rw,nosuid,nodev,relatime,user_id=1023,group_id=1023,default_permissions,allow_other 0 0
Why did you use /dev/block/vold
instead of /dev/fuse
in your code?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi friend,
Can in know how to implement this in phonegap... ?
as i am new to phonegap.. i need help..