Sunday, 31 May 2015

SDcard information or check current status

No comments
          Hello friends, in this android example i show you how to get storage information of SDcard, And check status of SDcard like its mounted or available in devise or not.here is simple code is to get Total size of SDcard and Remaining space.Lets go for coding.In this we get size of storage in GB,MB,KB and Byte.


CREATE NEW ANDROID PROJECT


Step 1: Write code into activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/state"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15dip"
        android:textStyle="bold"
        android:typeface="normal" >
    </TextView>

    <TextView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dip" >
    </TextView>

</LinearLayout>


Step 2:Write code into MainActivity.java


package dev.Androidapplink.sdcardinfoapp;

import java.text.NumberFormat;

import dev.Androidapplink.sdcardinfoapp.R;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.widget.TextView;

public class MainActivity extends Activity 
{
//the statistics of the SD card
private StatFs stats;
//the state of the external storage
private String externalStorageState;

//the total size of the SD card
private double totalSize;
//the available free space
private double freeSpace;

//a String to store the SD card information
private String outputInfo;
//a TextView to output the SD card state
private TextView tv_state;
//a TextView to output the SD card information
private TextView tv_info;

//set the number format output
private NumberFormat numberFormat;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //initialize the Text Views with the data at the main.xml file
        tv_state = (TextView)findViewById(R.id.state);
        tv_info = (TextView)findViewById(R.id.info);
        
        //get external storage (SD card) state
        externalStorageState = Environment.getExternalStorageState();
        
        //checks if the SD card is available in device 
        if(externalStorageState.equals(Environment.MEDIA_MOUNTED)
        || externalStorageState.equals(Environment.MEDIA_UNMOUNTED)
        || externalStorageState.equals(Environment.MEDIA_MOUNTED_READ_ONLY))
        {
        //obtain the stats from the root of the SD card.
        stats = new StatFs(Environment.getExternalStorageDirectory().getPath());
       
        //Add 'Total Size' to the output string:
        outputInfo = "\nTotal Size:\n";
       
        //total usable size
        totalSize = stats.getBlockCount() * stats.getBlockSize();
       
        //initialize the NumberFormat object
        numberFormat = NumberFormat.getInstance();
        //disable grouping
        numberFormat.setGroupingUsed(false);
        //display numbers with two decimal places
        numberFormat.setMaximumFractionDigits(2); 
       
        //SD card's total size in gigabytes, megabytes, kilobytes and bytes
        outputInfo += "Size in gigabytes: " + numberFormat.format((totalSize / (double)1073741824)) + " GB \n"
        + "Size in megabytes: " + numberFormat.format((totalSize / (double)1048576)) + " MB \n" 
        + "Size in kilobytes: " + numberFormat.format((totalSize / (double)1024)) + " KB \n" 
        + "Size in bytes: " +  numberFormat.format(totalSize) + " B \n"; 
       
        //Add 'Remaining Space' to the output string:
        outputInfo += "\nRemaining Space:\n";
       
        //available free space
        freeSpace = stats.getAvailableBlocks() * stats.getBlockSize();
       
        //SD card's available free space in gigabytes, megabytes, kilobytes and bytes
        outputInfo += "Size in gigabytes: " + numberFormat.format((freeSpace / (double)1073741824)) + " GB \n"
        + "Size in megabytes: " + numberFormat.format((freeSpace / (double)1048576)) + " MB \n" 
        + "Size in kilobytes: " + numberFormat.format((freeSpace / (double)1024)) + " KB \n"
        + "Size in bytes: " + numberFormat.format(freeSpace) + " B \n"; 
       
        //output the SD card state
        tv_state.setTextColor(Color.GREEN);
        tv_state.setText("SD card found! SD card is " + externalStorageState +".");
       
        //output the SD card info
        tv_info.setText(outputInfo);
        }
        else //external storage was not found
        {
        //output the SD card state
        tv_state.setTextColor(Color.RED);
        tv_state.setText("SD card not found! SD card state is \"" + externalStorageState + "\".");
        }
    }
}

NOTE:Give permission in AndroidManifest.xml

 <uses-permission android:name="android.permission.STORAGE" />


Step 3:Run project to see Output:





No comments :

Post a Comment

Follow me Share