我正在研究一个需要查看用户存储的图像的Android应用程序。问题是,如果用户通过USB电缆安装了SD卡,我无法读取磁盘上的图像列表。如何判断sdcard是否安装在Android中?
有谁知道一种方法来告诉如果安装了USB,以便我可以弹出一条消息,通知用户它将无法工作?
我正在研究一个需要查看用户存储的图像的Android应用程序。问题是,如果用户通过USB电缆安装了SD卡,我无法读取磁盘上的图像列表。如何判断sdcard是否安装在Android中?
有谁知道一种方法来告诉如果安装了USB,以便我可以弹出一条消息,通知用户它将无法工作?
如果你想访问设备上的图像,最好的方法是使用MediaStore content provider。访问它作为content provider将允许您查询存在的图像,并在适当的情况下将content://
URL映射到设备上的文件路径。
如果您仍需要访问SD卡,相机应用程序包括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;
}
An lo,有人提供了android api的做事方式。投票! – 2009-05-23 22:52:48
方法`checkFsWritable();`? – GrIsHu 2015-06-29 10:22:10
我很抱歉发布非Android方法来做到这一点,希望有人可以使用Android API提供答案。
您可以列出sdcard根目录下的文件。如果没有,则SD卡要么完全空白(不寻常,但可能)或未安装。如果你尝试在SD卡上创建一个空文件并且失败,这意味着你试图在SD卡的挂载点上创建一个文件,由于权限问题而被拒绝,所以你会知道sdcard不是安装。
是的,我知道这是丑陋....
public static boolean isSdPresent() {
return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}
这里是jargonjustin
的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();
}
我用光标从SD卡上检索图像,当没有SD卡插入设备游标为空。 事实上,当通过从设备上物理移除卡来卸载SD卡时,就是这种情况。这是我所使用的代码:
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
在jargonjustin的帖子:
方法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;
}
}
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
只是为了记录在案,将USB电缆是不要让SD卡容量卸载的唯一途径 - 在存储设置中卸载卡或物理移除卡也会执行此操作。而且,由t-mobile uk提供的8GB SD卡没有正确预格式化,因为它们只是在没有正确格式化的情况下才会一直未安装。我只是说,因为有时只是告诉用户拔掉电缆是不够的。 – 2009-05-23 18:16:08