我正在研究一個需要查看用戶存儲的圖像的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