KFB*_*KFB 16 android android-sdcard
我正在开发一个Android应用程序,需要查看用户存储的图像.问题是如果用户通过USB线安装了SD卡,我就无法读取磁盘上的图像列表.
有没有人知道如何判断usb是否已安装,以便我可以弹出一条消息通知用户它不起作用?
小智 38
如果您尝试访问设备上的图像,最好的方法是使用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;
}
Run Code Online (Sandbox Code Playgroud)
这是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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29958 次 |
| 最近记录: |