17

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

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

4

8 回答 8

41

如果您尝试访问设备上的图像,最好的方法是使用MediaStore 内容提供程序。将其作为内容提供者访问将允许您查询存在的图像,并将content://URL 映射到设备上适当的文件路径。

如果您仍需要访问 SD 卡,Camera 应用程序包含一个ImageUtils类,用于检查 SD 卡是否已安装,如下所示:

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;
}
于 2009-05-23T22:50:47.767 回答
9

这是jargonjustinpost中的 checkFsWritable 缺失函数

private static boolean checkFsWritable() {
        // Create a temporary file to see whether a volume is really writeable.
        // It's important not to put it in the root directory which may have a
        // limit on the number of files.
        String directoryName = Environment.getExternalStorageDirectory().toString() + "/DCIM";
        File directory = new File(directoryName);
        if (!directory.isDirectory()) {
            if (!directory.mkdirs()) {
                return false;
            }
        }
        return directory.canWrite();
    }
于 2012-06-20T03:33:05.333 回答
2

我很抱歉发布了一种非 Android 的方式,希望有人可以使用 Android API 提供答案。

您可以列出 sdcard 根目录上的文件。如果没有,则 sdcard 要么完全空白(不寻常,但可能),要么已卸载。如果您尝试在 sdcard 上创建一个空文件并且失败,这意味着您正在尝试在 sdcard 的挂载点创建一个文件,由于权限问题,该文件将被拒绝,因此您会知道 sdcard 不是安装。

是的,我知道这很丑……

于 2009-05-23T20:38:43.993 回答
2
public static boolean isSdPresent() {

return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

}
于 2011-09-27T07:03:35.383 回答
2

在 jargonjustin 的帖子中:

文件ImageManager.java

方法hasStorage -->

public static boolean hasStorage(boolean requireWriteAccess) {
        String state = Environment.getExternalStorageState();

        if (Environment.MEDIA_MOUNTED.equals(state)) {
            if (requireWriteAccess) {
                boolean writable = checkFsWritable();
                return writable;
            } else {
                return true;
            }
        } else if (!requireWriteAccess
                && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            return true;
        }
        return false;
    }

方法checkFsWritable -->

 private static boolean checkFsWritable() {
        // Create a temporary file to see whether a volume is really writeable.
        // It's important not to put it in the root directory which may have a
        // limit on the number of files.
        String directoryName =
                Environment.getExternalStorageDirectory().toString() + "/DCIM";
        File directory = new File(directoryName);
        if (!directory.isDirectory()) {
            if (!directory.mkdirs()) {
                return false;
            }
        }
        File f = new File(directoryName, ".probe");
        try {
            // Remove stale file if any
            if (f.exists()) {
                f.delete();
            }
            if (!f.createNewFile()) {
                return false;
            }
            f.delete();
            return true;
        } catch (IOException ex) {
            return false;
        }
    }
于 2013-08-11T15:29:15.170 回答
0

我正在使用光标从 sd 卡中检索图像,当设备中没有插入 sd 卡时,光标为空。实际上,这是通过从设备中物理移除卡来卸载 sdcard 卷的情况。这是我使用的代码:

  Cursor mCursor = this.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
    if (mCursor == null || !mCursor .moveToFirst()) {
                /**
                 *mCursor == null:
                 * - query failed; the app don't have access to sdCard; example: no sdCard 
                 *
                 *!mCursor.moveToFirst():
                 *     - there is no media on the device
                 */
            } else {
                 // process the images...
                 mCursor.close(); 
    }

更多信息:http: //developer.android.com/guide/topics/media/mediaplayer.html#viacontentresolver

于 2012-07-17T14:40:53.513 回答
0

在对外部存储进行任何操作之前,您应该始终调用 getExternalStorageState() 来检查媒体是否可用。媒体可能已安装到计算机、丢失、只读或处于其他状态。例如,您可以使用以下几种方法来检查可用性:

/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}

/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state) ||
        Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        return true;
    }
    return false;
}

来源:http: //developer.android.com/guide/topics/data/data-storage.html

于 2015-01-04T06:39:18.877 回答
-1
Cool....Check it out...
try {
            File mountFile = new File("/proc/mounts");
            usbFoundCount=0;
            sdcardFoundCount=0;
            if(mountFile.exists())
             {
                Scanner usbscanner = new Scanner(mountFile);
                while (usbscanner.hasNext()) {
                    String line = usbscanner.nextLine();
                    if (line.startsWith("/dev/fuse /storage/usbcard1")) {
                        usbFoundCount=1;
                        Log.i("-----USB--------","USB Connected and properly mounted---/dev/fuse /storage/usbcard1" );
                    }
            }
         }
            if(mountFile.exists()){
                Scanner sdcardscanner = new Scanner(mountFile);
                while (sdcardscanner.hasNext()) {
                    String line = sdcardscanner.nextLine();
                    if (line.startsWith("/dev/fuse /storage/sdcard1")) {
                        sdcardFoundCount=1;
                        Log.i("-----USB--------","USB Connected and properly mounted---/dev/fuse /storage/sdcard1" );
                    }
            }
         }
            if(usbFoundCount==1)
            {
                Toast.makeText(context,"USB Connected and properly mounted", 7000).show();
                Log.i("-----USB--------","USB Connected and properly mounted" );
            }
            else
            {
                Toast.makeText(context,"USB not found!!!!", 7000).show();
                Log.i("-----USB--------","USB not found!!!!" );

            }
            if(sdcardFoundCount==1)
            {
                Toast.makeText(context,"SDCard Connected and properly mounted", 7000).show();
                Log.i("-----SDCard--------","SDCard Connected and properly mounted" );
            }
            else
            {
                Toast.makeText(context,"SDCard not found!!!!", 7000).show();
                Log.i("-----SDCard--------","SDCard not found!!!!" );

            }
        }catch (Exception e) {
            e.printStackTrace();
        } 
于 2014-06-09T07:15:26.557 回答