3 回答
TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超4個(gè)贊
要獲取所有可用的外部存儲文件夾(包括SD卡),可以使用以下命令:
File[] externalStorageFiles=ContextCompat.getExternalFilesDirs(this,null);
要轉(zhuǎn)到每個(gè)外部存儲的“根目錄”(這是真正的安裝文件夾路徑),可以使用我已經(jīng)完成的以下功能:
/** 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貢獻(xiàn)1839條經(jīng)驗(yàn) 獲得超15個(gè)贊
Environment.getExternalStorageState() 返回內(nèi)部SD安裝點(diǎn)的路徑,例如“ / mnt / sdcard”
不,Environment.getExternalStorageDirectory()是指設(shè)備制造商認(rèn)為是“外部存儲”的任何內(nèi)容。在某些設(shè)備上,這是可移動(dòng)媒體,例如SD卡。在某些設(shè)備上,這是設(shè)備上閃存的一部分。此處,“外部存儲空間”是指“至少在Android 1.x和2.x上可以通過安裝在主機(jī)上的USB大容量存儲模式訪問的內(nèi)容”。
但是問題是關(guān)于外部SD。如何獲得“ / mnt / sdcard / external_sd”之類的路徑(可能因設(shè)備而異)?
如上所述,除了外部存儲,Android沒有“外部SD”的概念。
如果設(shè)備制造商選擇外部存儲為板載閃存,并且還具有SD卡,則需要與該制造商聯(lián)系,以確定是否可以使用SD卡(不保證)以及適用的規(guī)則。使用它,例如使用什么路徑。
試試看:
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 關(guān)注
- 694 瀏覽
添加回答
舉報(bào)
