在这里,我们正在为android电视盒开发android应用程序。如何获取已挂载的外部存储(如USB stick, SDCARD and SATA HDD.
)的列表
发布于 2015-05-23 00:01:56
要获取所有可用的外部存储文件夹(包括SD卡),您可以使用以下命令:
File[] externalStorageFiles=ContextCompat.getExternalFilesDirs(this,null);
要转到每个外部存储的“根”(即实际的挂载文件夹路径),您可以使用我创建的这个函数:
/** Given any file/folder inside an sd card, this will return the path of the sd card */
private static String getRootOfInnerSdCardFolder(File file)
{
if(file==null)
return null;
final long totalSpace=file.getTotalSpace();
while(true)
{
final File parentFile=file.getParentFile();
if(parentFile==null||parentFile.getTotalSpace()!=totalSpace)
return file.getAbsolutePath();
file=parentFile;
}
}
发布于 2012-02-18 19:37:21
Environment.getExternalStorageState()
返回内部SD挂载点的路径,如"/mnt/sdcard“
不,Environment.getExternalStorageDirectory()
指的是设备制造商认为是“外部存储”的任何东西。在某些设备上,这是可移动介质,如SD卡。在某些设备上,这是设备上闪存的一部分。在这里,“外部存储”指的是“安装在主机上时可通过USB大容量存储模式访问的内容”,至少在Android 1.x和2.x上是这样。
但问题是关于外部SD。如何获取"/mnt/sdcard/external_sd“这样的路径(不同设备可能会有所不同)?
Android没有“外部SD”的概念,除了外部存储,如上所述。
如果设备制造商选择使用板载闪存和SD卡,您需要与制造商联系,以确定您是否可以使用SD卡(不保证)以及使用规则,例如使用路径。
试试这个:
Manifest.xml:
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.ums_connected" />
</intent-filter>
</receiver>
我的接收者:
public class MyReceiver extends BroadcastReceiver{
if (intent.getAction().equalsIgnoreCase(
"android.intent.action.UMS_CONNECTED")) {...}
}
发布于 2016-06-16 19:59:23
另一种选择。
首先获取所有外部文件目录
File[] externalStorageFiles=ContextCompat.getExternalFilesDirs(this,null);
然后为每个文件夹调用此函数
private static String getRootOfExternalStorage(File file)
{
if (file == null)
return null;
String path = file.getAbsolutePath();
return path.replaceAll("/Android/data/" + getPackageName() + "/files", "");
}
https://stackoverflow.com/questions/9340332
复制相似问题