package com.example.sdoperation;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.text.format.Formatter;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.util.StateSet;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends ActionBarActivity implements OnClickListener{
private Button btnSaveSD;
private Button btnReadSD;
private Button btnGetSD;
private Button btnGetPhone;
private TextView tvMemory;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Get components
btnGetSD = (Button) findViewById(R.id.btn_get);
btnSaveSD = (Button)findViewById(R.id.btn_saveSD);
tvMemory = (TextView) findViewById(R.id.tv_memoray);
btnReadSD = (Button)findViewById(R.id.btn_readSD);
btnGetPhone = (Button)findViewById(R.id.btn_readPhone);
//Add click event
btnGetSD.setOnClickListener(this);
btnSaveSD.setOnClickListener(this);
btnReadSD.setOnClickListener(this);
btnGetPhone.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_saveSD:
saveFile2Sd();
Break;
case R.id.btn_readSD:
readFile2Sd();
Break;
case R.id.btn_get:
getSDDetail();
Break;
case R.id.btn_readPhone:
getPhoneDetail();
Break;
Default:
Break;
}
}
* *
*Read phone memory
* /
private void getPhoneDetail() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long blockCount = stat.getBlockCount();
long avilableBlocks = stat.getAvailableBlocks();
long totalSize = blockSize*blockCount;
long avilableSize = blockSize*avilableBlocks;
String totalSizeStr = Formatter.formatFileSize(this, totalSize);
String alilableSizeStr = Formatter.formatFileSize(this, avilableSize);
Tvmemory.settext ("total memory:" + totalsizestr + "available memory:" + alilablesizestr);
}
* *
*Read SD card memory
* /
private void getSDDetail() {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long blockCount = stat.getBlockCount();
long avilableBlocks = stat.getAvailableBlocks();
long totalSize = blockSize*blockCount;
long avilableSize = blockSize*avilableBlocks;
String totalSizeStr = Formatter.formatFileSize(this, totalSize);
String alilableSizeStr = Formatter.formatFileSize(this, avilableSize);
Tvmemory.settext ("total memory:" + totalsizestr + "available memory:" + alilablesizestr);
}
* *
*Write data to SD card
* /
private void saveFile2Sd() {
//Get SD card status
String sdState = Environment.getExternalStorageState();
if (sdState.equals(Environment.MEDIA_MOUNTED)) {
File file = new File(Environment.getExternalStorageDirectory(), "info.txt");
FileOutputStream fos;
Try {
fos = new FileOutputStream(file);
fos.write("xxx".getBytes());
Fos.close ();
Toast. Maketext (this, "save successfully", toast. Length_short). Show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Toast. Maketext (this, "SD card does not exist", toast. Length_short. Show();
}
}
* *
*Read data from SD card
* /
private void readFile2Sd() {
//Get SD card status
String sdState = Environment.getExternalStorageState();
if (sdState.equals(Environment.MEDIA_MOUNTED)) {
File file = new File(Environment.getExternalStorageDirectory(), "info.txt");
BufferedReader br;
Try {
br = new BufferedReader(new FileReader(file));
String content = br.readLine();
tvMemory.setText(content);
Br.close ();
Toast. Maketext (this, "read successfully", toast. Length_short). Show();
} catch (Exception e) {
Toast. Maketext (this, "SD card does not exist", toast. Length_short. Show();
}
} else {
Toast. Maketext (this, "SD card does not exist", toast. Length_short. Show();
}
}
}