2011-09-15 16 views
47

मेरा ऐप मोबाइल के लिए काम कर रहा है जिसमें केवल एक एसडी कार्ड है। तो प्रोग्रामेटिक रूप से मैं यह जांचना चाहता हूं कि एसडी कार्ड उपलब्ध है या नहीं और एसडी कार्ड मुक्त स्थान कैसे खोजें। क्या यह संभव है?जांचें कि एसडी कार्ड उपलब्ध है या प्रोग्रामेटिक रूप से

यदि हां, तो मैं इसे कैसे कर सकता हूं?

उत्तर

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

एसडीकार्ड मुफ्त मेमोरी को कैसे जांचें? – naresh

+1

धन्यवाद, यह – naresh

+40

पर काम कर रहा है, लेकिन अगर फोन में इनबिल्ड स्टोरेज है तो यह सच हो जाता है .. इसलिए सही उत्तर नहीं – User10001

12

उपयोग Environment.getExternalStorageState()"Using the External Storage" में वर्णित है।

बाह्य भंडारण पर उपलब्ध स्थान पाने के लिए, 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

मुझे लगता है कि भंडारण राज्य की जाँच के लिए एक छोटे से वर्ग में लिखा था। शायद यह आपके लिए कुछ उपयोग है।

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

क्या यह calss एसडी कार्ड उपलब्धता का पता लगाने में मदद करता है? –

+0

@PankajNimgade यह कक्षा आपको यह जांचने में सहायता करती है कि बाहरी संग्रहण उपलब्ध है या/या लिखने योग्य है या नहीं। बाहरी भंडारण एक एसडी कार्ड या अंतर्निहित भंडारण हो सकता है जैसे कि नेक्सस उपकरणों में। – kaolick

+0

विशेष रूप से "एसडीकार्ड" के लिए जांचना संभव है? अग्रिम धन्यवाद –

4

मैंने इसे संशोधित किया कि यदि कोई एसडी कार्ड मौजूद है, तो यह वहां पथ सेट करता है। यदि नहीं, तो यह इसे आंतरिक निर्देशिका में सेट करता है।

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

मैं अगर एसडी कार्ड पर फ़ोल्डर उपलब्ध है या नहीं की जाँच करने के एक वर्ग बनाया। सभी प्रकार के उपकरणों में परीक्षण किया गया।

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; 
    } 
    } 
0

यह सरल विधि मेरे लिए काम किया जाता है:

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

स्वीकृत जवाब के लिए मुझे

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

काम नहीं करता मामले में यदि डिवाइस में एक में निर्मित भंडारण, यह सच रिटर्न; मेरा समाधान बाहरी फाइल निर्देशिका गणना की जांच करने के लिए है, यदि एक से अधिक है, तो डिवाइस में sdcard है। यह काम करता है और मैंने कई उपकरणों के लिए इसका परीक्षण किया।

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

आप देख सकते हैं बाहरी हटाने योग्य एसडी कार्ड इस

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; 

} 

यह पूरी तरह से काम करता है के रूप में मैं यह परीक्षण किया है की तरह उपलब्ध है।

संबंधित मुद्दे