2011-09-15 52 views

Trả lời

120
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); 
Boolean isSDSupportedDevice = Environment.isExternalStorageRemovable(); 

if(isSDSupportedDevice && isSDPresent) 
{ 
    // yes SD-card is present 
} 
else 
{ 
// Sorry 
} 
+3

cách kiểm tra bộ nhớ sdcard miễn phí? – naresh

+1

cảm ơn bạn đang làm việc – naresh

+40

nhưng nó trở lại đúng nếu điện thoại có lưu trữ inbuild..so không phải là câu trả lời đúng – User10001

12

Sử dụng Environment.getExternalStorageState() như được mô tả trong "Using the External Storage".

Để có được không gian có sẵn trên lưu trữ bên ngoài, sử dụng StatFs:

// do this only *after* you have checked external storage state: 
File extdir = Environment.getExternalStorageDirectory(); 
File stats = new StatFs(extdir.getAbsolutePath()); 
int availableBytes = stats.getAvailableBlocks() * stats.getBlockSize(); 
3
void updateExternalStorageState() { 
    String state = Environment.getExternalStorageState(); 
    if (Environment.MEDIA_MOUNTED.equals(state)) { 
     mExternalStorageAvailable = mExternalStorageWriteable = true; 
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { 
     mExternalStorageAvailable = true; 
     mExternalStorageWriteable = false; 
    } else { 
     mExternalStorageAvailable = mExternalStorageWriteable = false; 
} 
handleExternalStorageState(mExternalStorageAvailable, 
     mExternalStorageWriteable); 
} 
5

Tôi đã viết một lớp ít cho rằng kiểm tra trạng thái lưu trữ. Có lẽ đó là một số sử dụng cho bạn.

import android.os.Environment; 

/** 
* Checks the state of the external storage of the device. 
* 
* @author kaolick 
*/ 
public class StorageHelper 
{ 
// Storage states 
private boolean externalStorageAvailable, externalStorageWriteable; 

/** 
* Checks the external storage's state and saves it in member attributes. 
*/ 
private void checkStorage() 
{ 
// Get the external storage's state 
String state = Environment.getExternalStorageState(); 

if (state.equals(Environment.MEDIA_MOUNTED)) 
{ 
    // Storage is available and writeable 
    externalStorageAvailable = externalStorageWriteable = true; 
} 
else if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) 
{ 
    // Storage is only readable 
    externalStorageAvailable = true; 
    externalStorageWriteable = false; 
} 
else 
{ 
    // Storage is neither readable nor writeable 
    externalStorageAvailable = externalStorageWriteable = false; 
} 
} 

/** 
* Checks the state of the external storage. 
* 
* @return True if the external storage is available, false otherwise. 
*/ 
public boolean isExternalStorageAvailable() 
{ 
checkStorage(); 

return externalStorageAvailable; 
} 

/** 
* Checks the state of the external storage. 
* 
* @return True if the external storage is writeable, false otherwise. 
*/ 
public boolean isExternalStorageWriteable() 
{ 
checkStorage(); 

return externalStorageWriteable; 
} 

/** 
* Checks the state of the external storage. 
* 
* @return True if the external storage is available and writeable, false 
*   otherwise. 
*/  
public boolean isExternalStorageAvailableAndWriteable() 
{ 
checkStorage(); 

if (!externalStorageAvailable) 
{ 
    return false; 
} 
else if (!externalStorageWriteable) 
{ 
    return false; 
} 
else 
{ 
    return true; 
} 
} 
} 
+0

hiện calss này giúp phát hiện thẻ sd sẵn có? –

+0

@PankajNimgade Lớp này giúp bạn kiểm tra xem bộ nhớ ngoài có sẵn và/hoặc ghi được không. Bộ nhớ ngoài có thể là thẻ SD hoặc bộ nhớ tích hợp như trong các thiết bị Nexus. – kaolick

+0

là nó có thể kiểm tra cụ thể cho các "sdcard" ?, Cảm ơn trước –

4

Tôi đã sửa đổi nó sao cho nếu thẻ SD tồn tại, nó đặt đường dẫn ở đó. Nếu không, nó đặt nó ở thư mục nội bộ.

Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); 
if(isSDPresent) 
{ 
    path = theAct.getExternalCacheDir().getAbsolutePath() + "/GrammarFolder"; 
} 
else 
{ 
    path = theAct.getFilesDir() + "/GrammarFolder"; 
} 
0

Tôi tạo ra một lớp để kiểm tra xem thư mục trên thẻ SD có sẵn hay không:

public class GetFolderPath { 

    static String folderPath; 

    public static String getFolderPath(Context context) { 
     if (isSdPresent() == true) { 
      try { 
       File sdPath = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/FolderName"); 
       if(!sdPath.exists()) { 
        sdPath.mkdirs(); 
        folderPath = sdPath.getAbsolutePath(); 
       } else if (sdPath.exists()) { 
        folderPath = sdPath.getAbsolutePath(); 
       } 
      } 
      catch (Exception e) { 

      } 
      folderPath = Environment.getExternalStorageDirectory().getPath()+"/FolderName/"; 
     } 
     else { 
      try { 
       File cacheDir=new File(context.getCacheDir(),"FolderName/"); 
       if(!cacheDir.exists()) { 
        cacheDir.mkdirs(); 
        folderPath = cacheDir.getAbsolutePath(); 
       } else if (cacheDir.exists()) { 
        folderPath = cacheDir.getAbsolutePath(); 
       } 
      } 
      catch (Exception e){ 

      } 
     } 
     return folderPath; 
    } 

    public static boolean isSdPresent() { 
     return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); 
    } 
} 
0

phương pháp đơn giản này được làm việc cho tôi. Thử nghiệm ở tất cả các loại thiết bị.

public boolean externalMemoryAvailable() { 
    if (Environment.isExternalStorageRemovable()) { 
     //device support sd card. We need to check sd card availability. 
     String state = Environment.getExternalStorageState(); 
     return state.equals(Environment.MEDIA_MOUNTED) || state.equals(
      Environment.MEDIA_MOUNTED_READ_ONLY); 
    } else { 
     //device not support sd card. 
     return false; 
    } 
    } 
9

câu trả lời được chấp nhận không làm việc cho tôi

Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); 

Trong trường hợp nếu thiết bị đã tích hợp sẵn trong lưu trữ, nó sẽ trả về true; Giải pháp của tôi là để kiểm tra số lượng thư mục tệp bên ngoài, nếu có nhiều thư mục, thiết bị có sdcard. Nó hoạt động và tôi đã thử nghiệm nó cho một số thiết bị.

public static boolean hasRealRemovableSdCard(Context context) { 
    return ContextCompat.getExternalFilesDirs(context, null).length >= 2; 
} 
2

Bạn có thể kiểm tra xem bên ngoài thẻ sd di động có sẵn như thế này

public static boolean externalMemoryAvailable(Activity context) { 
    File[] storages = ContextCompat.getExternalFilesDirs(context, null); 
    if (storages.length > 1 && storages[0] != null && storages[1] != null) 
     return true; 
    else 
     return false; 

} 

này hoạt động hoàn hảo như tôi đã thử nghiệm nó.

Các vấn đề liên quan