Android中如何判断SD卡是否挂载?
我正在开发一个 Android 应用程序,需要查看用户存储了哪些图像。 问题是,如果用户通过 USB 电缆安装了 SD 卡,我无法读取磁盘上的图像列表。
有谁知道有什么方法可以判断 USB 是否已安装,以便我可以弹出一条消息,通知用户它无法工作?
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您尝试访问设备上的图像,最好的方法是使用 MediaStore 内容提供商。 作为内容提供商访问它将允许您查询图像并在适当的情况下将
content://
URL 映射到设备上的文件路径。如果您仍然需要访问 SD 卡,相机应用程序包含 ImageUtils 类,用于检查 SD 卡是否安装如下:
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.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:
这是
jargonjustin
帖子中缺少的 checkFsWritable 函数Here is the checkFsWritable missing function in
jargonjustin
post我很抱歉发布了一种非 Android 方式来执行此操作,希望有人可以使用 Android API 提供答案。
您可以列出 SD 卡根目录下的文件。 如果没有,则 SD 卡要么完全空白(不常见,但可能),要么已卸载。 如果您尝试在 sdcard 上创建一个空文件并且失败,则意味着您尝试在 sdcard 的挂载点中创建一个文件,该文件会因权限问题而被拒绝,因此您会知道 sdcard 不是安装。
是的,我知道这很丑......
I apologise for posting a non-Android way of doing this, hopefully someone can provide an answer using the Android API.
You could list the files on the root of the sdcard. If there is none, the sdcard is either entirely blank (unusual, but possible) or it is unmounted. If you try to create an empty file on the sdcard and it fails, it means that you were trying to create a file in the mount-point of the sdcard which would be denied due to a permissions issue so you would know the sdcard was not mounted.
Yes, I know this is ugly....
在行话贾斯汀的帖子上:
文件 ImageManager.java
On jargonjustin's post:
File ImageManager.java
我使用光标从 SD 卡检索图像,当设备中没有插入 SD 卡时,光标为空。
实际上,这是通过从设备中物理移除卡来卸载 SD 卡卷时的情况。 这是我使用过的代码:
更多信息:http://developer .android.com/guide/topics/media/mediaplayer.html#viacontentresolver
I was using cursor for retrieving the images from sd card and when no sd card was inserted in device the cursor was null.
Actually this is the case when the sdcard volume was unmounted by physically removing card from device. This is the code I've used:
More info: http://developer.android.com/guide/topics/media/mediaplayer.html#viacontentresolver
在对外部存储进行任何操作之前,您应该始终调用 getExternalStorageState() 来检查媒体是否可用。 该介质可能已安装到计算机、丢失、只读或处于某种其他状态。 例如,您可以使用以下几种方法来检查可用性:
来源:
http://developer.android.com/guide/topics/data/data -存储.html
Before you do any work with the external storage, you should always call getExternalStorageState() to check whether the media is available. The media might be mounted to a computer, missing, read-only, or in some other state. For example, here are a couple methods you can use to check the availability:
source:
http://developer.android.com/guide/topics/data/data-storage.html