Android设备上外部SD卡的文件路径始终是" / storage / extSdCard" ?如果没有,有多少变化?
我需要在我的应用中测试外部 SD卡的可用性。
我正在使用Titanium,它有一个方法 Titanium.Filesystem.isExternalStoragePresent() 但即使没有安装外部SD卡,它也总能返回。
我认为它在本地存储中检测到SDCard因此返回true。但我真正想要的是检测物理SD卡是否已安装。
我可以通过单独检测" / storage / extSdCard" 文件来执行此操作吗?
感谢。
答案 0 :(得分:5)
Android设备上外部SDCard的文件路径始终是“/ storage / extSdCard”吗?如果没有,有多少变化?
遗憾的是,根据制造商的说法,外部存储的路径并不总是相同。使用Environment.getExternalStorageDirectory()
将返回SD卡的正常路径mnt/sdcard/
。但是对于三星设备,SD卡路径位于mnt/extSdCard/
或mnt/external_sd/
下。
因此,一种方法是根据每个制造商使用的路径检查外部目录是否存在。有这样的事情:
mExternalDirectory = Environment.getExternalStorageDirectory()
.getAbsolutePath();
if (android.os.Build.DEVICE.contains("samsung")
|| android.os.Build.MANUFACTURER.contains("samsung")) {
File f = new File(Environment.getExternalStorageDirectory()
.getParent() + "/extSdCard" + "/myDirectory");
if (f.exists() && f.isDirectory()) {
mExternalDirectory = Environment.getExternalStorageDirectory()
.getParent() + "/extSdCard";
} else {
f = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/external_sd" + "/myDirectory");
if (f.exists() && f.isDirectory()) {
mExternalDirectory = Environment
.getExternalStorageDirectory().getAbsolutePath()
+ "/external_sd";
}
}
}
但我真正想要的是检测物理SD卡是否已安装。
我还没有尝试过代码,但Dmitriy Lozenko在这个answer中的方法更有趣。 无论制造商是谁,他的方法都会返回系统上所有已安装SD卡的路径。
答案 1 :(得分:5)
我希望它对你有用:)
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;
}
}
答案 2 :(得分:2)
这就是我最终使用sdcard路径的方式:
public String getExternalStoragePath() {
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 = new File(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;
} else if (filePath.toLowerCase().contains("sdcard")) {
return filePath;
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
try {
if (Environment.isExternalStorageRemovable(file)) {
return filePath;
}
} catch (RuntimeException e) {
Log.e(TAG, "RuntimeException: " + e);
}
}
}
}
return null;
}
答案 3 :(得分:1)
我在4天后得到了解决方案,请注意以下几点,同时给出Android(Java)中File类的路径:
WITH CTE (Code, DuplicateCount)
AS
(
SELECT subscriber_id,
ROW_NUMBER() OVER(PARTITION BY subscriber_id
ORDER BY subscriber_id) AS DuplicateCount
FROM dbo.consumer_subsc
where subscriber_id in (3031103)
and status=1 and year(getdate()) >= year(start_date)
and year(getdate()) <= year(end_date)
)
Select * from CTE
path="/storage/sdcard0/myfile.txt";
path="/storage/sdcard1/myfile.txt";
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
e.g:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
答案 4 :(得分:1)
我刚才想出了什么。至少对于我的Android模拟器,我有像'/ storage / ????这样的SD卡路径 - ???? '其中每个?是大写字母或数字。
因此,如果 / storage / 目录中有一个可读且不是内部存储目录的目录,必须是SD卡。
我的代码在我的android模拟器上运行了!
String removableStoragePath;
File fileList[] = new File("/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.
如果有SD卡, removableStoragePath 将拥有它的路径。如果没有,那将是一个空字符串。
答案 5 :(得分:1)
最简单的方法,安装此免费文件浏览器并导航到您的SD卡。您将在顶部阅读路径。 在我的情况下,路径为:storage / extsd / extsd01 / https://play.google.com/store/apps/details?id=com.smartwho.SmartFileManager