我需要我的App来测试外部SDCard的可用性.
我使用的是Titanium,它有一个方法Titanium.filesystem.isExternalStoragePresent()
但是即使外部SDCard未挂载,它始终返回true.
我认为它在本地存储中检测到SDCard,因此返回true.但我真正想要的是检测物理SDCard是否挂载.
可以通过单独检测文件“/ storage / extSdCard”的存在来做到这一点吗?
谢谢.
@H_301_13@解决方法 我希望对你有用:)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;public class MemoryStorage { private MemoryStorage() {} 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(); 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); } } } } catch (Exception e) { e.printstacktrace(); } try { file voldfile = new file("/system/etc/vold.fstab"); if (voldfile.exists()) { Scanner scanner = new Scanner(voldfile); while (scanner.hasNext()) { String line = scanner.nextline(); 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); } } } } catch (Exception e) { e.printstacktrace(); } 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()); } return map; }}@H_301_13@ @H_301_13@ 总结
以上是内存溢出为你收集整理的Android的外部SDCard文件路径全部内容,希望文章能够帮你解决Android的外部SDCard文件路径所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)