Micro SD卡检测

发布于 2024-12-01 01:42:48 字数 76 浏览 4 评论 0原文

有没有办法在android中检测micro sd卡?我知道环境类提供了外部存储详细信息。但它只提供内置 SD 卡的详细信息。有办法解决吗?

Is there a way to detect a micro sd card in android? I know the Environment class gives external storage details. But it just gives the in built sd card details. Is there a way around?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

亣腦蒛氧 2024-12-08 01:42:48

您可以使用 isExternalStorageEmulated() 找出当前的“外部”存储实际上是真正的外部存储还是只是内部存储的一部分。如果它是真实的,那么您应该获得可移动卡的属性。

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. If it's real then you should get the properties of the removable card.

等风来 2024-12-08 01:42:48

试试这个:

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

Try this:

boolean canSaveExternal = false;
String storageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(storageState))
    canSaveExternal = true;
else
    canSaveExternal = false;
就此别过 2024-12-08 01:42:48

Environment.getExternalStorageState()和Environment.getExternalStorageDirectory()将提供内置SD卡,目前几乎所有Android设备上都存在该卡。

获取“真正的”外部 SD 卡(或 USB 磁盘)的两种方法。

  1. 使用 getVolumeList() 函数列出所有可移动存储,记得在访问之前检查挂载状态。

    private static String getExtendedMemoryPath(Context context) {
        StorageManager mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
        类存储卷Clazz = null;
        尝试 {
            storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
            方法 getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
            方法 getPath = storageVolumeClazz.getMethod("getPath");
            方法 isRemovable = storageVolumeClazz.getMethod("isRemovable");
            对象结果 = getVolumeList.invoke(mStorageManager);
            最终 int 长度 = Array.getLength(结果);
            for (int i = 0; i < 长度; i++) {
                对象存储VolumeElement = Array.get(结果, i);
                字符串路径 = (String) getPath.invoke(storageVolumeElement);
                布尔可移动 = (Boolean) isRemovable.invoke(storageVolumeElement);
                如果(可移除){
                    返回路径;
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InitationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        返回空值;
    }
    
  2. 注册android.intent.action.MEDIA_MOUNTED事件,当挂载存储时,会通过挂载的磁盘路径广播此intent。

    <接收器 android:enabled="true" android:name=".MountStatusReceiver">
            <意图过滤器>
                <动作 android:name="android.intent.action.MEDIA_MOUNTED"/>
                <数据 android:scheme="文件"/>
            
        
    
    @覆盖
    公共无效onReceive(上下文上下文,意图意图){
        如果(意图!=空){
                if (Intent.ACTION_MEDIA_MOUNTED.equals(intent.getAction())) {
                    路径 = Intent.getDataString().replace("file://", "");
                }
            }
        }
    }
    

Environment.getExternalStorageState() and Environment.getExternalStorageDirectory() will provide the built-in SD card, which is almost existing on all currently Android devices now.

Two methods to get the "real" external SD card (or USB disk).

  1. Use the getVolumeList() function to list all removable storages, remember to check the mount state before you access it.

    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.

    <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://", "");
                }
            }
        }
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文