簡體   English   中英

Micro Sd卡檢測

[英]Micro Sd card detection

有沒有辦法在Android中檢測micro sd卡? 我知道Environment類提供了外部存儲細節。 但它只是給出了內置的SD卡細節。 有辦法嗎?

您可以使用isExternalStorageEmulated()來查明當前的“外部”存儲實際上是真正的外部存儲還是僅僅是內部存儲的一部分。 如果它是真的那么你應該得到可移動卡的屬性。

嘗試這個:

boolean canSaveExternal = false;
String storageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(storageState))
    canSaveExternal = true;
else
    canSaveExternal = false;

Environment.getExternalStorageState()和Environment.getExternalStorageDirectory()將提供內置的SD卡,現在幾乎存在於所有當前的Android設備上。

獲得“真正的”外部SD卡(或USB磁盤)的兩種方法。

  1. 使用getVolumeList()函數列出所有可移動存儲,請記住在訪問之前檢查安裝狀態。

     private static String getExtendedMemoryPath(Context context) { StorageManager mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); Class<?> storageVolumeClazz = null; try { storageVolumeClazz = Class.forName("android.os.storage.StorageVolume"); Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList"); Method getPath = storageVolumeClazz.getMethod("getPath"); Method isRemovable = storageVolumeClazz.getMethod("isRemovable"); Object result = getVolumeList.invoke(mStorageManager); final int length = Array.getLength(result); for (int i = 0; i < length; i++) { Object storageVolumeElement = Array.get(result, i); String path = (String) getPath.invoke(storageVolumeElement); boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement); if (removable) { return path; } } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return null; } 
  2. 注冊android.intent.action.MEDIA_MOUNTED事件,當掛載存儲時,將使用掛載的磁盤路徑廣播此intent。

     <receiver android:enabled="true" android:name=".MountStatusReceiver"> <intent-filter> <action android:name="android.intent.action.MEDIA_MOUNTED"/> <data android:scheme="file"/> </intent-filter> </receiver> @Override public void onReceive(Context context, Intent intent) { if (intent != null) { if (Intent.ACTION_MEDIA_MOUNTED.equals(intent.getAction())) { path = intent.getDataString().replace("file://", ""); } } } } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM