Read and write data to SD card in Android, reading SD card and phone memory

Source: Internet
Author: User


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();
}
}
} 
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.