如何判断SD卡是否安装在Android?

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

有谁知道一个方法来告诉如果安装的USB,以便我可以popup一条消息,通知用户,它将无法正常工作?

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

如果您仍然需要访问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; } 

这里是jargonjustin文章中的checkFsWritable缺less的函数

 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(); } 

我很抱歉发布非Android的方式做到这一点,希望有人可以提供一个使用Android API的答案。

您可以列出SD卡根目录中的文件。 如果没有,SD卡要么是完全空白(不寻常,但可能)或未安装。 如果你尝试在SD卡上创build一个空文件,并且失败了,这意味着你试图在SD卡的挂载点上创build一个文件,由于权限问题,这个文件将被拒绝,所以你会知道sdcard不是安装。

是的,我知道这是丑陋的….

 public static boolean isSdPresent() { return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); } 

我用光标检索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

 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(); } 

在使用外部存储器之前,您应该始终调用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

在jargonjustin的post里:

文件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; } }