如何判断SD卡是否安装在Android中?
我正在开发一个Android应用程序,它需要查看用户存储了哪些图像。问题是,如果用户通过USB电缆安装SD卡,我无法读取磁盘上的图像列表如何判断SD卡是否安装在Android中?,android,android-sdcard,Android,Android Sdcard,我正在开发一个Android应用程序,它需要查看用户存储了哪些图像。问题是,如果用户通过USB电缆安装SD卡,我无法读取磁盘上的图像列表 有没有人知道一种方法可以告诉我是否安装了usb,这样我就可以弹出一条消息通知用户它无法工作?我很抱歉发布了一种非Android的方法,希望有人能使用Android API提供答案 您可以在SD卡的根目录上列出文件。如果没有,SD卡要么完全空白(不常见,但可能),要么已卸载。如果尝试在SD卡上创建一个空文件但失败,则表示您试图在SD卡的装入点创建一个文件,该文件
有没有人知道一种方法可以告诉我是否安装了usb,这样我就可以弹出一条消息通知用户它无法工作?我很抱歉发布了一种非Android的方法,希望有人能使用Android API提供答案 您可以在SD卡的根目录上列出文件。如果没有,SD卡要么完全空白(不常见,但可能),要么已卸载。如果尝试在SD卡上创建一个空文件但失败,则表示您试图在SD卡的装入点创建一个文件,该文件将由于权限问题而被拒绝,以便您知道SD卡未装入
是的,我知道这很难看….我很抱歉发布了一个非Android的方式来做这件事,希望有人能使用Android API提供答案 您可以在SD卡的根目录上列出文件。如果没有,SD卡要么完全空白(不常见,但可能),要么已卸载。如果尝试在SD卡上创建一个空文件但失败,则表示您试图在SD卡的装入点创建一个文件,该文件将由于权限问题而被拒绝,以便您知道SD卡未装入
是的,我知道这很难看….如果您试图访问设备上的图像,最好的方法是使用。将其作为访问将允许您查询存在的图像,并将
内容://
URL映射到设备上适当的文件路径
如果您仍然需要访问SD卡,相机应用程序将包括一个类,用于检查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;
}
如果您试图访问设备上的图像,最好的方法是使用。将其作为访问将允许您查询存在的图像,并将
内容://
URL映射到设备上适当的文件路径
如果您仍然需要访问SD卡,相机应用程序将包括一个类,用于检查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
post中的checkfswriteable缺失函数
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();
}
下面是
jargonjustin
post中的checkfswriteable缺失函数
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卡卷是通过从设备中物理移除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();
}
更多信息:我使用游标从sd卡检索图像,当设备中没有插入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();
}
更多信息:关于贾斯汀的帖子: 文件 方法--> 方法-->
在贾斯汀的帖子上: 文件 方法--> 方法-->
在使用外部存储器之前,应始终调用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;
}
资料来源:
在使用外部存储器之前,应始终调用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;
}
资料来源:
请注意,插上USB电缆并不是卸载SD卡卷的唯一方法-在存储设置中卸载SD卡或物理卸下SD卡也可以。此外,t-mobile uk提供的8GB SD卡未正确预格式化,因此由于I/O原因,只要未正确重新格式化,它们就会一直卸载。我之所以这么说,是因为有时仅仅告诉用户拔下电缆是不够的。请记住,拔下USB电缆并不是卸下SD卡卷的唯一方法-在存储设置中卸下SD卡或物理卸下SD卡也可以。此外,t-mobile uk提供的8GB SD卡未正确预格式化,因此由于I/O原因,只要未正确重新格式化,它们就会一直卸载。我这么说只是因为有时候仅仅告诉用户拔下电缆是不够的。瞧,有人提供了android api的操作方式。投票!其中是方法
checkfswriteable()代码>?我们需要
让它在外部存储上工作吗?瞧,有人提供了android api的工作方式。投票!其中是方法checkfswriteable()代码>?我们需要
使其在外部存储器上工作吗?@MartinSmith那里的源代码也有me@MartinSmith那里的来源也是我