[英]Get external storage path for USB-flash with Android 6.0.1
在Nexus 5X上使用Android 6.0.1(API23),
不同的Android版本以及如何访问外部存储(在我的情况下为USB-Stick)可能会非常令人困惑。 据我了解,您需要在清单中授予权限,如下所示:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
之后,您应该可以访问外部存储。
这是我使用“ getExternalFilesDirs()”方法获取外部存储路径的方法。 但是事实证明,只有内部SD卡可以识别(即,该方法返回的第一个数组元素)。
根据我的阅读,如果有更多的外部存储设备连接到手机,则应该使用“ getExternalFilesDirs()”方法返回更多的数组元素。 但就我而言,它们都不可用。
这是我的代码:
String strInfo = "";
File folders[] = getExternalFilesDirs(null);
// File folders[] = ContextCompat.getExternalFilesDirs(this,null);
strInfo += "\ngetExternalFilesDirs(null):\n";
for(File f : folders){
strInfo += f.getAbsolutePath() + "\n";
}
if (folders.length > 1) {
Toast.makeText(this, "nr of folders = " + folders.length + "/ info = " + strInfo, Toast.LENGTH_SHORT).show();
File myFile = new File(folders[1], "testfile.txt");
} else {
// !!!!!!!!!!!!! Keep ending up in this case, even tough a USB-stick is
// connected to the phone (...also tried with a second SD-card
// connected...but same thing, keeeping up ending here...
// any idea WHY ???????????????
Toast.makeText(this, "No external storage device found / info = " + strInfo, Toast.LENGTH_SHORT).show();
}
最终,我想在外部连接的USB记忆棒上创建文件和文件夹。 有人可以告诉我一种可靠的方法,该方法如何在连接到Android-6.0.1手机的USB记忆棒上创建新文件和文件夹吗? 我很感激。
若要解决此问题:不幸的是,USB记忆棒似乎不能用作Android手机的辅助存储(=外部存储)。
我刚刚找到此链接 ,说:
External storage devices returned here are considered a permanent part
of the device, including both emulated external storage and physical
media slots, such as SD cards in a battery compartment. The returned
paths do not include transient devices, such as USB flash drives.
同样, 有关getExternalFilesDirs()的Android官方文档说 :
The returned paths do not include transient devices, such as USB flash drives
connected to handheld devices.
如果有人知道确实能满足次级存储要求的C型USB记忆棒,请告诉我! 同样,如果有人知道可用作次存储的USB-C型SD卡适配器,请告诉我...
太糟糕了,我无法将文件和文件夹从我的Java android应用程序写入外部USB记忆棒:(
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.