View

Thursday, 8 September 2011

Phone internal and External storage usage

How to check Phone internal/external memory usage using programmatically in android

Android have a default API to check the phone internal and external memory usage.In My application
i used both as well as memory informations.The internal and external both are same,just to change
the Environment path.
Internal=StatFs stat=new StatFs(Environment.getDataDirectory().getPath());
External=StatFs stat=new StatFs(Environment.getExternalStorageDirectory().getPath());
The user can just hit the seek bar its show the occupied space in bytes,availabel space in MB,
Before that first to create new emulatore with size can set it in Kb or MB.I used 24MB.
public class Internal_Storage extends Activity  implements SeekBar.OnSeekBarChangeListener{
 SeekBar sb;
 int progress;
 Boolean value=false;
 Button press_btn;
 TextView availabel_in_tv,occupied_in_tv,tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.internal_storage);
        availabel_in_tv=(TextView)findViewById(R.id.availabel_in_tv);
        occupied_in_tv=(TextView)findViewById(R.id.occupied_in_tv);
        final TextView reading = (TextView) findViewById(R.id.textView1);
        sb = (SeekBar)findViewById(R.id.sb);
        sb.setMax(100);
    @Override
    public void onProgressChanged(SeekBar seekBar, int size, boolean fromtouch) {
        reading.setText("Loading "+size+"% ");
        method1();
       
        }
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
       
        }
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }
  });
    }
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
    }
 @Override
 public void onStartTrackingTouch(SeekBar seekBar) {
  // TODO Auto-generated method stub

 }
 @Override
 public void onStopTrackingTouch(SeekBar seekBar) {
  // TODO Auto-generated method stub
 }
public String method1(){
StatFs stat=new StatFs(Environment.getDataDirectory().getPath()); //**ITS show  phone internal  storage size**//
       or
StatFs stat=new StatFs(Environment.getExternalStorageDirectory().getPath());  //**ITS show  phone SD card storage size**//

    int blockSize = stat.getBlockSize();
    int availableBlocks = stat.getAvailableBlocks();     
    System.out.println("statFs:"+"=" +stat);
    System.out.println("SPACE AVAILABEL :"+"=" +availableBlocks);
     //occup_size_tv.setTextSize(availableBlocks);
     occupied_in_tv.setText(Integer.toString(availableBlocks));
     System.out.println("SIZE OCCUPY:"+"=" +blockSize);
  
     availabel_in_tv.setText((Formatter.formatFileSize(this,availableBlocks*blockSize)));
     System.out.println("FREE SPACE:="+Formatter.formatFileSize(this,availableBlocks*blockSize));
     System.out.println("FREE SPACE IN BYTES:"+availableBlocks +1042.f*1024.f);
     // bytesAvailable / (1024.f * 1024.f);
     return Formatter.formatShortFileSize(this, availableBlocks * blockSize);


 }
the same code repeated for internal and external.class
Then next class Memory_Information.class,its show total memory usage details
public class Memory_Information extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.memory_information);   
        TextView CPUinfo = (TextView) findViewById(R.id.textView1);
        CPUinfo.setText(ReadCPUinfo());    
    }
    private String ReadCPUinfo()
    {
     ProcessBuilder cmd;
   
     StringBuffer strMemory = new StringBuffer();
     //final ActivityManager activityManager =(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
   
     ActivityManager actvityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
     ActivityManager.MemoryInfo mInfo = new ActivityManager.MemoryInfo ();
     actvityManager.getMemoryInfo( mInfo );
   
     strMemory.append("Available Memory : ");
     strMemory.append(mInfo.availMem);
     strMemory.append("\n");
     strMemory.append("\n");
   
     String result=strMemory.toString();
   
     try{
      String[] args = {"/system/bin/cat", "/proc/meminfo"};
      cmd = new ProcessBuilder(args);
   
      Process process = cmd.start();
      InputStream in = process.getInputStream();
      byte[] re = new byte[1024];
      while(in.read(re) != -1){
       System.out.println(new String(re));
       result = result + new String(re);
      }
      in.close();
     } catch(IOException ex){
      ex.printStackTrace();
     }
     return result;
    }
}
you may check in console also or u can clarify the usage in phones settings. go to settings/SD card & phone storage/


How to check Phone internal/external memory usage using programmatically in android

Android have a default API to check the phone internal and external memory usage.In My application
i used both as well as memory informations.The internal and external both are same,just to change
the Environment path.
Internal=StatFs stat=new StatFs(Environment.getDataDirectory().getPath());
External=StatFs stat=new StatFs(Environment.getExternalStorageDirectory().getPath());
The user can just hit the seek bar its show the occupied space in bytes,availabel space in MB,
Before that first to create new emulatore with size can set it in Kb or MB.I used 24MB.
public class Internal_Storage extends Activity  implements SeekBar.OnSeekBarChangeListener{
 SeekBar sb;
 int progress;
 Boolean value=false;
 Button press_btn;
 TextView availabel_in_tv,occupied_in_tv,tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.internal_storage);
        availabel_in_tv=(TextView)findViewById(R.id.availabel_in_tv);
        occupied_in_tv=(TextView)findViewById(R.id.occupied_in_tv);
        final TextView reading = (TextView) findViewById(R.id.textView1);
        sb = (SeekBar)findViewById(R.id.sb);
        sb.setMax(100);
    @Override
    public void onProgressChanged(SeekBar seekBar, int size, boolean fromtouch) {
        reading.setText("Loading "+size+"% ");
        method1();
       
        }
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
       
        }
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }
  });
    }
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
    }
 @Override
 public void onStartTrackingTouch(SeekBar seekBar) {
  // TODO Auto-generated method stub

 }
 @Override
 public void onStopTrackingTouch(SeekBar seekBar) {
  // TODO Auto-generated method stub
 }
public String method1(){
StatFs stat=new StatFs(Environment.getDataDirectory().getPath()); //**ITS show  phone internal  storage size**//
       or
StatFs stat=new StatFs(Environment.getExternalStorageDirectory().getPath());  //**ITS show  phone SD card storage size**//

    int blockSize = stat.getBlockSize();
    int availableBlocks = stat.getAvailableBlocks();     
    System.out.println("statFs:"+"=" +stat);
    System.out.println("SPACE AVAILABEL :"+"=" +availableBlocks);
     //occup_size_tv.setTextSize(availableBlocks);
     occupied_in_tv.setText(Integer.toString(availableBlocks));
     System.out.println("SIZE OCCUPY:"+"=" +blockSize);
  
     availabel_in_tv.setText((Formatter.formatFileSize(this,availableBlocks*blockSize)));
     System.out.println("FREE SPACE:="+Formatter.formatFileSize(this,availableBlocks*blockSize));
     System.out.println("FREE SPACE IN BYTES:"+availableBlocks +1042.f*1024.f);
     // bytesAvailable / (1024.f * 1024.f);
     return Formatter.formatShortFileSize(this, availableBlocks * blockSize);


 }
the same code repeated for internal and external.class
Then next class Memory_Information.class,its show total memory usage details
public class Memory_Information extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.memory_information);   
        TextView CPUinfo = (TextView) findViewById(R.id.textView1);
        CPUinfo.setText(ReadCPUinfo());    
    }
    private String ReadCPUinfo()
    {
     ProcessBuilder cmd;
   
     StringBuffer strMemory = new StringBuffer();
     //final ActivityManager activityManager =(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
   
     ActivityManager actvityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
     ActivityManager.MemoryInfo mInfo = new ActivityManager.MemoryInfo ();
     actvityManager.getMemoryInfo( mInfo );
   
     strMemory.append("Available Memory : ");
     strMemory.append(mInfo.availMem);
     strMemory.append("\n");
     strMemory.append("\n");
   
     String result=strMemory.toString();
   
     try{
      String[] args = {"/system/bin/cat", "/proc/meminfo"};
      cmd = new ProcessBuilder(args);
   
      Process process = cmd.start();
      InputStream in = process.getInputStream();
      byte[] re = new byte[1024];
      while(in.read(re) != -1){
       System.out.println(new String(re));
       result = result + new String(re);
      }
      in.close();
     } catch(IOException ex){
      ex.printStackTrace();
     }
     return result;
    }
}
you may check in console also or u can clarify the usage in phones settings. go to settings/SD card & phone storage/