एंड्रॉइड को आंतरिक / बाहरी मेमोरी का मुफ्त आकार मिलता है


98

मैं अपने डिवाइस के आंतरिक / बाह्य भंडारण पर मुफ्त मेमोरी का आकार प्रोग्रामेटिक रूप से प्राप्त करना चाहता हूं। मैं इस कोड का उपयोग कर रहा हूँ:

StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long bytesAvailable = (long)stat.getBlockSize() *(long)stat.getBlockCount();
long megAvailable = bytesAvailable / 1048576;
Log.e("","Available MB : "+megAvailable);

File path = Environment.getDataDirectory();
StatFs stat2 = new StatFs(path.getPath());
long blockSize = stat2.getBlockSize();
long availableBlocks = stat2.getAvailableBlocks();
String format =  Formatter.formatFileSize(this, availableBlocks * blockSize);
Log.e("","Format : "+format);

और जो परिणाम मुझे मिल रहा है वह है:

11-15 10:27:18.844: E/(25822): Available MB : 7572
11-15 10:27:18.844: E/(25822): Format : 869MB

समस्या यह है कि मैं SdCard की मुफ्त मेमोरी प्राप्त करना चाहता हूं जो 1,96GBअभी है। मैं इस कोड को कैसे ठीक कर सकता हूं ताकि मुझे मुफ्त आकार मिल सके?


एपीआई स्तर 18 के रूप में उन्होंने लॉन्ग के साथ समाप्त होने की विधि का नाम बदल दिया है। संभवतः आपको इससे पहले एपीआई स्तर का एक चेक जोड़ने की आवश्यकता होगी
जयशिल दवे

सभी समाधान मैंने किसी को भी काम नहीं करने की कोशिश की, जब मैं आंतरिक भंडारण के रूप में प्रारूपित करता हूं ... क्या आप मुझे खुश कर सकते हैं, इसे कैसे प्राप्त करें?
योगेश राठी

जवाबों:


182

नीचे अपने उद्देश्य के लिए कोड है:

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

    public static String getAvailableInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSizeLong();
        long availableBlocks = stat.getAvailableBlocksLong();
        return formatSize(availableBlocks * blockSize);
    }

    public static String getTotalInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSizeLong();
        long totalBlocks = stat.getBlockCountLong();
        return formatSize(totalBlocks * blockSize);
    }

    public static String getAvailableExternalMemorySize() {
        if (externalMemoryAvailable()) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSizeLong();
            long availableBlocks = stat.getAvailableBlocksLong();
            return formatSize(availableBlocks * blockSize);
        } else {
            return ERROR;
        }
    }

    public static String getTotalExternalMemorySize() {
        if (externalMemoryAvailable()) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSizeLong();
            long totalBlocks = stat.getBlockCountLong();
            return formatSize(totalBlocks * blockSize);
        } else {
            return ERROR;
        }
    }

    public static String formatSize(long size) {
        String suffix = null;

        if (size >= 1024) {
            suffix = "KB";
            size /= 1024;
            if (size >= 1024) {
                suffix = "MB";
                size /= 1024;
            }
        }

        StringBuilder resultBuffer = new StringBuilder(Long.toString(size));

        int commaOffset = resultBuffer.length() - 3;
        while (commaOffset > 0) {
            resultBuffer.insert(commaOffset, ',');
            commaOffset -= 3;
        }

        if (suffix != null) resultBuffer.append(suffix);
        return resultBuffer.toString();
    }

RAM आकार प्राप्त करें

ActivityManager actManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
MemoryInfo memInfo = new ActivityManager.MemoryInfo();
actManager.getMemoryInfo(memInfo);
long totalMemory = memInfo.totalMem;

2
getBlockSize()और getBlockCountपदावनत हो जाते हैं।
नीमा जी

2
@DineshPrajapati उत्तर के लिए धन्यवाद, मैं प्रश्न हैं, अगर मैं) मोबाइल मेमोरी की गणना के लिए Environment.getRootDirectory (का उपयोग Environment.getDataDirectory बजाय-की, मैं कुछ उत्पादन हो रही है ..this आंतरिक मेमोरी अन्य स्मृति को संदर्भित करता है ..
एके जोशी

3
@DineshPrajapati .. मोटो जी 2 पर परीक्षण बाहरी संग्रहण के लिए गलत डेटा प्राप्त करना
एके जोशी

1
नए API स्तरों (> 18) के लिए अंत में लॉन्ग का उपयोग करें
Gun2sh

1
ज्ञान बांटने के लिए बहुत-बहुत धन्यवाद
किशन सोनी '

40

यह मैंने ऐसा किया है:

StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long bytesAvailable;
if (android.os.Build.VERSION.SDK_INT >= 
    android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
    bytesAvailable = stat.getBlockSizeLong() * stat.getAvailableBlocksLong();
}
else {
    bytesAvailable = (long)stat.getBlockSize() * (long)stat.getAvailableBlocks();
}
long megAvailable = bytesAvailable / (1024 * 1024);
Log.e("","Available MB : "+megAvailable);

2
लेकिन यह प्रतिशोधित है :(
abbasalim

@ ArMo372, क्या आप लोगों ने इसके लिए प्रतिस्थापन कोड का पता लगाया?
सिंपल कोडर

3
बस getBlockSizeऔर के getAvailableBlocksसाथ बदलें getBlockSizeLongऔर getAvailableBlocksLong
smg

1
इसे सही जगह नहीं मिल रही है। यह 1678 @smg के बजाय 1141 मिल रहा है

1
समाधान नहीं काम कर रहा है जब मैं आंतरिक भंडारण के रूप में प्रारूपित करता हूं ... क्या आप मुझे खुश कर सकते हैं, इसे कैसे प्राप्त किया जाए
योगेश राठी

27

एपीआई 9 के बाद से आप कर सकते हैं:

long freeBytesInternal = new File(ctx.getFilesDir().getAbsoluteFile().toString()).getFreeSpace();
long freeBytesExternal = new File(getExternalFilesDir(null).toString()).getFreeSpace();

2
File.getUsableSpace () शायद बेहतर है क्योंकि आप शायद रूट के रूप में नहीं चल रहे हैं।
मार्क

File.getUsableSpace()दिखता है एक आसान विधि का उपयोग कर के बजाय का उपयोग करना चाहते StatFs। मैं StatFs@MarkCarter का उपयोग क्यों करूंगा?
स्टुइरलिंग

1
@ डिस्को 2 अगर आप अपने minSdkVersion 9 से कम है, तो स्टेटफ का इस्तेमाल करेंगे।
मार्क

1
आप संग्रहण परिवर्तनों की निगरानी भी कैसे करेंगे?
एंड्रॉइड डेवलपर

24

सभी उपलब्ध संग्रहण फ़ोल्डर (एसडी कार्ड सहित) प्राप्त करने के लिए, आपको सबसे पहले भंडारण फाइलें मिलेंगी:

File internalStorageFile=getFilesDir();
File[] externalStorageFiles=ContextCompat.getExternalFilesDirs(this,null);

फिर आप उनमें से प्रत्येक का उपलब्ध आकार प्राप्त कर सकते हैं।

इसे करने के 3 तरीके हैं:

एपीआई 8 और नीचे:

StatFs stat=new StatFs(file.getPath());
long availableSizeInBytes=stat.getBlockSize()*stat.getAvailableBlocks();

एपीआई 9 और ऊपर:

long availableSizeInBytes=file.getFreeSpace();

एपीआई 18 और उससे ऊपर (पिछले एक की जरूरत नहीं है तो ठीक है):

long availableSizeInBytes=new StatFs(file.getPath()).getAvailableBytes(); 

अब आपको जो मिला है उसका एक अच्छा स्वरूपित स्ट्रिंग प्राप्त करने के लिए, आप इसका उपयोग कर सकते हैं:

String formattedResult=android.text.format.Formatter.formatShortFileSize(this,availableSizeInBytes);

या आप इसका उपयोग उस स्थिति में कर सकते हैं जब आप सटीक बाइट संख्या देखना चाहते हैं, लेकिन अच्छी तरह से:

NumberFormat.getInstance().format(availableSizeInBytes);

ध्यान दें कि मुझे लगता है कि आंतरिक भंडारण पहले बाहरी भंडारण के समान हो सकता है, क्योंकि पहले एक का अनुकरण किया जाता है।


EDIT: एंड्रॉइड Q और इसके बाद के संस्करण पर StorageVolume का उपयोग करना, मुझे लगता है कि प्रत्येक का मुफ्त स्थान प्राप्त करना संभव है, जैसे कुछ का उपयोग करना:

    val storageManager = getSystemService(Context.STORAGE_SERVICE) as StorageManager
    val storageVolumes = storageManager.storageVolumes
    AsyncTask.execute {
        for (storageVolume in storageVolumes) {
            val uuid: UUID = storageVolume.uuid?.let { UUID.fromString(it) } ?: StorageManager.UUID_DEFAULT
            val allocatableBytes = storageManager.getAllocatableBytes(uuid)
            Log.d("AppLog", "allocatableBytes:${android.text.format.Formatter.formatShortFileSize(this,allocatableBytes)}")
        }
    }

मुझे यकीन नहीं है कि यह सही है, और मुझे प्रत्येक के कुल आकार को प्राप्त करने का कोई तरीका नहीं मिल रहा है, इसलिए मैंने इसके बारे में यहां लिखा , और यहां इसके बारे में पूछा ।


1
एपीआई 23 के साथ उपकरणों पर हटाने योग्य एसडी कार्ड (या यूएसबी ओटीजी फ्लैश ड्राइव) पर मुफ्त स्थान कैसे प्राप्त करें? नया StatFs (file.getPath ())। getAvailableBytes () या file.getUsableSpace () 972546048 बाइट देता है जो नेक्सस 5 (मार्शमैलो 6.0.1) पर वास्तविक भंडारण आकार की परवाह किए बिना है।
जासूसी

@isabsent Nexus 5 में एसडी कार्ड स्लॉट नहीं है। आपने इसकी जाँच कैसे की?
Android डेवलपर

मैंने इसे USB OTG फ्लैश ड्राइव से चेक किया है।
isabsent

@isabsent मैंने कभी इसका इस्तेमाल नहीं किया। माफ़ करना। क्या यह एपीआई 22 और उससे नीचे पर अच्छा काम करता है?
Android डेवलपर

1
@Smeet क्या यह संभव है कि आप इसे Android 6 या इसके बाद के संस्करण पर आज़माएँ? यदि हां, तो शायद यह इस तरह का एक मुद्दा है: code.google.com/p/android/issues/detail?id=200326
Android डेवलपर


7

इस सरल स्निपेट को आज़माएं

    public static String readableFileSize() {
    long availableSpace = -1L;
    StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2)
        availableSpace = (long) stat.getBlockSizeLong() * (long) stat.getAvailableBlocksLong();
    else
        availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();

    if(availableSpace <= 0) return "0";
    final String[] units = new String[] { "B", "kB", "MB", "GB", "TB" };
    int digitGroups = (int) (Math.log10(availableSpace)/Math.log10(1024));
    return new DecimalFormat("#,##0.#").format(availableSpace/Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}

धन्यवाद, लेकिन मेरे पास java.lang.ArrayIndexOutOfBoundsException: length=5; index=-2147483648त्रुटि है, ऐसा लगता है कि digitGroupsपरिणाम -2147483648 है।
एक्यूना

समाधान काम नहीं कर रहा है जब मैं आंतरिक भंडारण के रूप में प्रारूपित करता हूं ... क्या आप मुझे खुश कर सकते हैं, इसे कैसे प्राप्त करें
योगेश राठी

6

यदि आपको आंतरिक और बाहरी संग्रहण पथ मिलता है, तो उपलब्ध संग्रहण का पता लगाना बहुत आसान है। इसके अलावा फोन के बाहरी भंडारण पथ का उपयोग करना बहुत आसान है

। Environment.getExternalStorageDirectory () getPath ();

इसलिए मैं केवल इस बात पर ध्यान केंद्रित कर रहा हूं कि कैसे हटाने योग्य एसडीकार्ड, यूएसबी ओटीजी (नहीं यूएसबी ओटीजी का परीक्षण किया जाए क्योंकि मेरे पास कोई यूएसबी ओटीजी नहीं है) जैसे बाहरी हटाने योग्य भंडारण के मार्ग का पता लगाने के लिए।

नीचे विधि सभी संभव बाहरी हटाने योग्य भंडारण पथों की एक सूची देगी।

 /**
     * This method returns the list of removable storage and sdcard paths.
     * I have no USB OTG so can not test it. Is anybody can test it, please let me know
     * if working or not. Assume 0th index will be removable sdcard path if size is
     * greater than 0.
     * @return the list of removable storage paths.
     */
    public static HashSet<String> getExternalPaths()
    {
    final HashSet<String> out = new HashSet<String>();
    String reg = "(?i).*vold.*(vfat|ntfs|exfat|fat32|ext3|ext4).*rw.*";
    String s = "";
    try
    {
        final Process process = new ProcessBuilder().command("mount").redirectErrorStream(true).start();
        process.waitFor();
        final InputStream is = process.getInputStream();
        final byte[] buffer = new byte[1024];
        while (is.read(buffer) != -1)
        {
            s = s + new String(buffer);
        }
        is.close();
    }
    catch (final Exception e)
    {
        e.printStackTrace();
    }

    // parse output
    final String[] lines = s.split("\n");
    for (String line : lines)
    {
        if (!line.toLowerCase(Locale.US).contains("asec"))
        {
            if (line.matches(reg))
            {
                String[] parts = line.split(" ");
                for (String part : parts)
                {
                    if (part.startsWith("/"))
                    {
                        if (!part.toLowerCase(Locale.US).contains("vold"))
                        {
                            out.add(part.replace("/media_rw","").replace("mnt", "storage"));
                        }
                    }
                }
            }
        }
    }
    //Phone's external storage path (Not removal SDCard path)
    String phoneExternalPath = Environment.getExternalStorageDirectory().getPath();

    //Remove it if already exist to filter all the paths of external removable storage devices
    //like removable sdcard, USB OTG etc..
    //When I tested it in ICE Tab(4.4.2), Swipe Tab(4.0.1) with removable sdcard, this method includes
    //phone's external storage path, but when i test it in Moto X Play (6.0) with removable sdcard,
    //this method does not include phone's external storage path. So I am going to remvoe the phone's
    //external storage path to make behavior consistent in all the phone. Ans we already know and it easy
    // to find out the phone's external storage path.
    out.remove(phoneExternalPath);

    return out;
}

जैसा कि मुझे याद है, पथों के संचालन के लिए निरंतर नामों का उपयोग करना कुछ उपकरणों पर काम नहीं कर सकता है, क्योंकि कुछ के अपने रास्ते हो सकते हैं। मुझे उम्मीद है कि यह मामला नहीं है। प्रयास के लिए +1।
Android डेवलपर

1
@androiddeveloper वोटिंग के लिए आपका धन्यवाद। मुझे आपके डिवाइस में इस कोड का परीक्षण करने के लिए सभी के समर्थन की आवश्यकता है क्योंकि मेरे पास सभी डिवाइस नहीं हैं लेकिन 4 अलग-अलग उपकरणों में परीक्षण किया गया है और ठीक काम कर रहा है। कृपया टिप्पणी यहाँ किसी भी शरीर के मोबाइल में काम नहीं कर रही है।
स्मेट

समाधान नहीं काम कर रहा है जब मैं आंतरिक भंडारण के रूप में प्रारूपित करता हूं ... क्या आप मुझे खुश कर सकते हैं, इसे कैसे प्राप्त किया जाए
योगेश राठी

4

बाहरी मेमोरी विषय के लिए त्वरित अतिरिक्त

externalMemoryAvailable()दिनेश प्रजापति के जवाब में विधि नाम से भ्रमित न हों ।

Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())आपको मीडिया की वर्तमान स्थिति देता है, अगर मीडिया मौजूद है और माउंट / पॉइंट पर रीड / राइट एक्सेस के साथ आरोहित है। आपको trueनेक्सस 5 जैसे बिना एसडी-कार्ड वाले डिवाइस पर भी मिलेगा , लेकिन फिर भी यह स्टोरेज के साथ किसी भी ऑपरेशन से पहले एक 'होना चाहिए' पद्धति है।

यह जांचने के लिए कि आपके डिवाइस पर एसडी-कार्ड है या नहीं, आप विधि का उपयोग कर सकते हैं ContextCompat.getExternalFilesDirs()

यह क्षणिक उपकरणों को नहीं दिखाता है, जैसे कि USB फ्लैश ड्राइव।

यह भी जान लें कि ContextCompat.getExternalFilesDirs()एंड्रॉइड 4.3 और लोअर पर हमेशा केवल 1 प्रविष्टि (एसडी-कार्ड अगर यह उपलब्ध है, अन्यथा आंतरिक) वापस आ जाएगी। आप इसके बारे में और अधिक यहाँ पढ़ सकते हैं ।

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

मेरे मामले में यह पर्याप्त था, लेकिन यह मत भूलो कि कुछ Android उपकरणों में 2 एसडी-कार्ड हो सकते हैं, इसलिए यदि आपको उन सभी की आवश्यकता है - तो ऊपर दिए गए कोड को समायोजित करें।


2
@RequiresApi(api = Build.VERSION_CODES.O)
private void showStorageVolumes() {
    StorageStatsManager storageStatsManager = (StorageStatsManager) getSystemService(Context.STORAGE_STATS_SERVICE);
    StorageManager storageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
    if (storageManager == null || storageStatsManager == null) {
        return;
    }
    List<StorageVolume> storageVolumes = storageManager.getStorageVolumes();
    for (StorageVolume storageVolume : storageVolumes) {
        final String uuidStr = storageVolume.getUuid();
        final UUID uuid = uuidStr == null ? StorageManager.UUID_DEFAULT : UUID.fromString(uuidStr);
        try {
            Log.d("AppLog", "storage:" + uuid + " : " + storageVolume.getDescription(this) + " : " + storageVolume.getState());
            Log.d("AppLog", "getFreeBytes:" + Formatter.formatShortFileSize(this, storageStatsManager.getFreeBytes(uuid)));
            Log.d("AppLog", "getTotalBytes:" + Formatter.formatShortFileSize(this, storageStatsManager.getTotalBytes(uuid)));
        } catch (Exception e) {
            // IGNORED
        }
    }
}

StorageStatsManager वर्ग ने Android O और इसके बाद का संस्करण पेश किया जो आपको बाहरी / आंतरिक संग्रहण में निःशुल्क और कुल बाइट दे सकता है। स्रोत कोड के साथ विस्तृत के लिए, आप मेरे निम्नलिखित लेख को पढ़ सकते हैं। आप Android O से कम परावर्तन का उपयोग कर सकते हैं

https://medium.com/cashify-engineering/how-to-get-storage-stats-in-android-o-api-26-4b92eca6805b


2

इस तरह से मैंने इसे किया ।।

आंतरिक कुल मेमोरी

double totalSize = new File(getApplicationContext().getFilesDir().getAbsoluteFile().toString()).getTotalSpace();
double totMb = totalSize / (1024 * 1024);

आंतरिक मुक्त आकार

 double availableSize = new File(getApplicationContext().getFilesDir().getAbsoluteFile().toString()).getFreeSpace();
    double freeMb = availableSize/ (1024 * 1024);

बाहरी मुक्त और कुल मेमोरी

 long freeBytesExternal =  new File(getExternalFilesDir(null).toString()).getFreeSpace();
       int free = (int) (freeBytesExternal/ (1024 * 1024));
        long totalSize =  new File(getExternalFilesDir(null).toString()).getTotalSpace();
        int total= (int) (totalSize/ (1024 * 1024));
       String availableMb = free+"Mb out of "+total+"MB";

0

बाहरी संधि के बारे में, एक और तरीका है:
File external = Environment.getExternalStorageDirectory(); free:external.getFreeSpace(); total:external.getTotalSpace();


0

अलग-अलग सॉल्यूशन लिखने के लिए कोड लिखने के बाद यह खोजने के लिए पूरा कोड है

  • कुल बाहरी मेमोरी
  • मुफ्त बाहरी मेमोरी
  • बाहरी मेमोरी का उपयोग किया
  • टोटल इंटरनल मेमोरी
  • आंतरिक मेमोरी का उपयोग किया
  • नि: शुल्क आंतरिक मेमोरी

'' ''

object DeviceMemoryUtil {
private const val error: String = "Something went wrog"
private const val noExternalMemoryDetected = "No external Storage detected"
private var totalExternalMemory: Long = 0
private var freeExternalMemory: Long = 0
private var totalInternalStorage: Long = 0
private var freeInternalStorage: Long = 0

/**
 * Checks weather external memory is available or not
 */
private fun externalMemoryAvailable(): Boolean {
    return Environment.getExternalStorageState() ==
            Environment.MEDIA_MOUNTED
}

/**
 *Gives total external memory
 * @return String Size of external memory
 * @return Boolean True if memory size is returned
 */
fun getTotalExternalMemorySize(): Pair<String?, Boolean> {
    val dirs: Array<File> = ContextCompat.getExternalFilesDirs(CanonApplication.getCanonAppInstance(), null)
    return if (externalMemoryAvailable()) {
        if (dirs.size > 1) {
            val stat = StatFs(dirs[1].path)
            val blockSize = stat.blockSizeLong
            val totalBlocks = stat.blockCountLong
            var totalExternalSize = totalBlocks * blockSize
            totalExternalMemory = totalExternalSize
            Pair(formatSize(totalExternalSize), true)
        } else {
            Pair(error, false)
        }
    } else {
        Pair(noExternalMemoryDetected, false)
    }
}

/**
 * Gives free external memory size
 * @return String Size of free external memory
 * @return Boolean True if memory size is returned
 */
fun getAvailableExternalMemorySize(): Pair<String?, Boolean> {
    val dirs: Array<File> = ContextCompat.getExternalFilesDirs(CanonApplication.getCanonAppInstance(), null)
    if (externalMemoryAvailable()) {
        return if (dirs.size > 1) {
            val stat = StatFs(dirs[1].path)
            val blockSize = stat.blockSizeLong
            val availableBlocks = stat.availableBlocksLong
            var freeExternalSize = blockSize * availableBlocks
            freeExternalMemory = freeExternalSize
            Pair(formatSize(freeExternalSize), true)
        } else {
            Pair(error, false)
        }
    } else {
        return Pair(noExternalMemoryDetected, false)
    }
}

/**
 * Gives used external memory size
 *  @return String Size of used external memory
 * @return Boolean True if memory size is returned
 */
fun getUsedExternalMemorySize(): Pair<String?, Boolean> {
    return if (externalMemoryAvailable()) {
        val totalExternalSize = getTotalExternalMemorySize()
        val freeExternalSize = getAvailableExternalMemorySize()
        if (totalExternalSize.second && freeExternalSize.second) {
            var usedExternalVolume = totalExternalMemory - freeExternalMemory
            Pair(formatSize(usedExternalVolume), true)
        } else {
            Pair(error, false)
        }
    } else {
        Pair(noExternalMemoryDetected, false)
    }
}

/**
 *Formats the long to size of memory in gb,mb etc.
 * @param size Size of memory
 */
fun formatSize(size: Long): String? {
    return android.text.format.Formatter.formatFileSize(CanonApplication.getCanonAppInstance(), size)
}

/**
 * Gives total internal memory size
 *  @return String Size of total internal memory
 * @return Boolean True if memory size is returned
 */
fun getTotalInternalStorage(): Pair<String?, Boolean> {
    if (showStorageVolumes()) {
        return Pair(formatSize(totalInternalStorage), true)
    } else {
        return Pair(error, false)
    }

}

/**
 * Gives free or available internal memory size
 *  @return String Size of free internal memory
 * @return Boolean True if memory size is returned
 */
fun getFreeInternalStorageVolume(): Pair<String?, Boolean> {
    return if (showStorageVolumes()) {
        Pair(formatSize(freeInternalStorage), true)
    } else {
        Pair(error, false)
    }
}

/**
 *For calculation of internal storage
 */
private fun showStorageVolumes(): Boolean {
    val storageManager = CanonApplication.canonApplicationInstance.applicationContext.getSystemService(Context.STORAGE_SERVICE) as StorageManager
    val storageStatsManager = CanonApplication.canonApplicationInstance.applicationContext.getSystemService(Context.STORAGE_STATS_SERVICE) as StorageStatsManager
    if (storageManager == null || storageStatsManager == null) {
        return false
    }
    val storageVolumes: List<StorageVolume> = storageManager.storageVolumes
    for (storageVolume in storageVolumes) {
        var uuidStr: String? = null
        storageVolume.uuid?.let {
            uuidStr = it
        }
        val uuid: UUID = if (uuidStr == null) StorageManager.UUID_DEFAULT else UUID.fromString(uuidStr)
        return try {
            freeInternalStorage = storageStatsManager.getFreeBytes(uuid)
            totalInternalStorage = storageStatsManager.getTotalBytes(uuid)
            true
        } catch (e: Exception) {
            // IGNORED
            false
        }
    }
    return false
}

fun getTotalInternalExternalMemory(): Pair<Long?, Boolean> {
    if (externalMemoryAvailable()) {
        if (getTotalExternalMemorySize().second) {
            if (getTotalInternalStorage().second) {
                return Pair(totalExternalMemory + totalInternalStorage, true)
            } else {
                return Pair(0, false)
            }
        }
        return Pair(0, false)
    } else {
        if (getTotalInternalStorage().second) {
            return Pair(totalInternalStorage, true)
        } else {
            return Pair(0, false)
        }
    }

}

fun getTotalFreeStorage(): Pair<Long,Boolean> {
    if (externalMemoryAvailable()){
        if(getFreeInternalStorageVolume().second){
            getFreeInternalStorageVolume()
            getAvailableExternalMemorySize()
                return Pair(freeExternalMemory + freeInternalStorage,true)
        }
        else{
            return Pair(0,false)
        }
    }
    else {
        if (getFreeInternalStorageVolume().second){
            getFreeInternalStorageVolume()
            return Pair(freeInternalStorage,true)
        }
      else{
            return Pair(0,false)
        }
    }

}}
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.