3 回答
TA貢獻1851條經驗 獲得超4個贊
要獲取所有可用的外部存儲文件夾(包括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;
}
}
TA貢獻1839條經驗 獲得超15個贊
Environment.getExternalStorageState() 返回內部SD安裝點的路徑,例如“ / mnt / sdcard”
不,Environment.getExternalStorageDirectory()是指設備制造商認為是“外部存儲”的任何內容。在某些設備上,這是可移動媒體,例如SD卡。在某些設備上,這是設備上閃存的一部分。此處,“外部存儲空間”是指“至少在Android 1.x和2.x上可以通過安裝在主機上的USB大容量存儲模式訪問的內容”。
但是問題是關于外部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")) {...}
}
- 3 回答
- 0 關注
- 695 瀏覽
添加回答
舉報
