简体   繁体   English

Micro Sd卡检测

[英]Micro Sd card detection

Is there a way to detect a micro sd card in android? 有没有办法在Android中检测micro sd卡? I know the Environment class gives external storage details. 我知道Environment类提供了外部存储细节。 But it just gives the in built sd card details. 但它只是给出了内置的SD卡细节。 Is there a way around? 有办法吗?

You can use isExternalStorageEmulated() to find out if the current "external" storage is actually a real external storage or just part of the internal storage. 您可以使用isExternalStorageEmulated()来查明当前的“外部”存储实际上是真正的外部存储还是仅仅是内部存储的一部分。 If it's real then you should get the properties of the removable card. 如果它是真的那么你应该得到可移动卡的属性。

Try this: 尝试这个:

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

Environment.getExternalStorageState() and Environment.getExternalStorageDirectory() will provide the built-in SD card, which is almost existing on all currently Android devices now. Environment.getExternalStorageState()和Environment.getExternalStorageDirectory()将提供内置的SD卡,现在几乎存在于所有当前的Android设备上。

Two methods to get the "real" external SD card (or USB disk). 获得“真正的”外部SD卡(或USB磁盘)的两种方法。

  1. Use the getVolumeList() function to list all removable storages, remember to check the mount state before you access it. 使用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. Register android.intent.action.MEDIA_MOUNTED event, when a storage is mounted, will broadcast this intent with the mounted disk path. 注册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