Android SD card status, path, free space, memory

Source: Internet
Author: User

SD card status, path, free space byte, reference http://ai4work.blog.163.com/blog/static/189873306201182310304586/

Memory, reference http://blog.csdn.net/hudashi/article/details/7050897

Manifest permissions:

<!--Create and delete files in SDcard permissions--<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/ > <!--write data to SDcard permissions--<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

Code

package com.test;import android.app.activity;import android.app.activitymanager;import  android.os.bundle;import android.os.environment;import android.os.statfs;import  android.util.log;public class test2 extends activity {    @ Override    protected void oncreate (bundle savedinstancestate)  {         super.oncreate (savedinstancestate);         setcontentview (r.layout.main);         int i  = 0;        while  (i < 1)  {             try {                 system.out.println ("1: "  +  Checksdcard ());                 string path =  Getexternalstoragepath ();                 system.out.println ("2: "  + path);                 if  (path != null)  {                      System.out.println ("3: "  +  (Getavailablestore (path) >>20)  +  "MB");                 }                 displaybriefmemory ();             } catch  (exception e)  {           &Nbsp;     e.printstacktrace ();             }            i++;         }    }    //Memory Related      private void displaybriefmemory ()  {             final ActivityManager activityManager =  (Activitymanager)   Getsystemservice (Activity_service);             Activitymanager.memoryinfo info = new activitymanager.memoryinfo ();            activitymanager.getmemoryinfo (Info);     &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.I ("System.out",  "System remaining Memory:" + (info.availmem > &GT;&NBSP;20) + "MB");   &nbsP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.I ("System.out",  "System is in low memory operation:" +info.lowmemory); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.I ("System.out",  "when system remaining memory is less than"  +  ( INFO.THRESHOLD&NBSP;&GT;&GT;&NBSP;20)  +  "MB as low Memory run");    }          //  Check SD card status     private boolean  Checksdcard ()  {        if  ( Environment.getexternalstoragestate (). Equals (                 environment.media_mounted))  {             return true;        }         return false;    }    / /  get the SD card path     public static string getexteRnalstoragepath ()  {        //  get sdcard status          string state = android.os.environment.getexternalstoragestate ();         //  determine if sdcard exists and is available          if  (Android.os.Environment.MEDIA_MOUNTED.equals (state))  {             if  (Android.os.Environment.getExternalStorageDirectory (). CanWrite ())  {                 return android.os.environment.getexternalstoragedirectory ()                          . GetPath ();            }         }         return null;    }    / **     *  *  Get the remaining capacity of the memory card in bytes      *       *  *  @param  filePath     *       *  *  @return  availableSpare     *       *       */    public static long  getavailablestore (String filepath)  {        //  Gets the SDcard file path         statfs statfs = new statfs ( FilePath);        //  get Block's size         long blocsize = statfs.getblocksize ();         //  Get BNumber of lock         // long totalBlocks =  Statfs.getblockcount ();        //  number of blocks available for use          long availablock = statfs.getavailableblocks ();         // long total = totalBlocks * blocSize;         long availableSpare = availaBlock *  blocsize;        return availablespare;    }}


Android SD card status, path, free space, memory

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.