如何判断sdcard是否安装在Android中? [英] How to tell if the sdcard is mounted in Android?

查看:34
本文介绍了如何判断sdcard是否安装在Android中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要查看用户存储的图像的 Android 应用程序.问题是,如果用户通过 USB 线安装了 sdcard,我无法读取磁盘上的图像列表.

I'm working on an Android application that needs to look at what images a user has stored. The problem is that if the user has the sdcard mounted via the USB cable, I can't read the list of images on the disk.

有谁知道如何判断 USB 是否已安装,以便我可以弹出一条消息通知用户它无法工作?

Does anyone know of a way to tell if the usb is mounted so that I could just pop up a message informing the user that it won't work?

推荐答案

如果您尝试访问设备上的图像,最好的方法是使用 MediaStore 内容提供程序.以 content provider 访问它,您将可以查询图像存在,并将 content:// URL 映射到设备上适当的文件路径.

If you're trying to access images on the device, the best method is to use the MediaStore content provider. Accessing it as a content provider will allow you to query the images that are present, and map content:// URLs to filepaths on the device where appropriate.

如果您仍然需要访问 SD 卡,Camera 应用程序包含一个

If you still need to access the SD card, the Camera application includes an ImageUtils class that checks if the SD card is mounted as follows:

static public boolean hasStorage(boolean requireWriteAccess) {
    //TODO: After fix the bug,  add "if (VERBOSE)" before logging errors.
    String state = Environment.getExternalStorageState();
    Log.v(TAG, "storage state is " + state);

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        if (requireWriteAccess) {
            boolean writable = checkFsWritable();
            Log.v(TAG, "storage writable is " + writable);
            return writable;
        } else {
            return true;
        }
    } else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        return true;
    }
    return false;
}

这篇关于如何判断sdcard是否安装在Android中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆