View source code find code to get the remaining capacity of SD card

Source: Internet
Author: User

  • Download Android source code, find the app under the Settings application source code, import settings project (Android project source code)
  • find "free space" to get

      <string name= "memory_available" msgid= "418542433817289474" > "Free Space" </STRING>  
  • Find "memory_available" and get

    <Preference android:key="memory_sd_avail"     style="?android:attr/preferenceInformationStyle"     android:title="@string/memory_available"    android:summary="00"/>
  • Find "MemorySDavail" to get

    //这个字符串就是sd卡剩余容量formatSize(availableBlocks * blockSize) + readOnly//这两个参数相乘,得到sd卡以字节为单位的剩余容量availableBlocks * blockSize
  • The storage device is divided into chunks, each with a fixed size

  • Chunk Size * Number of chunks equals the total size of the storage device
  • Sample code

    File Path =environment.getexternalstoragedirectory (); StatFs Stat=NewStatFs (Path.getpath ()); LongblockSize; Longtotalblocks; Longavailableblocks; //gets the level of the current system version    if(Build.VERSION.SDK_INT >=Build.version_codes. JELLY_BEAN_MR2) {blockSize=Stat.getblocksizelong (); Totalblocks=Stat.getblockcountlong (); Availableblocks=Stat.getavailableblockslong (); }    Else{blockSize=stat.getblocksize (); Totalblocks=Stat.getblockcount (); Availableblocks=stat.getavailableblocks (); } TextView TV=(TextView) Findviewbyid (r.id.tv); Tv.settext (Formatsize (availableblocks*blockSize));}PrivateString Formatsize (Longsize) {    returnFormatter.formatfilesize ( This, size);}

View source code find code to get the remaining capacity of SD card

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.