Android中向SD卡读写数据,读SD卡和手机内存

时间:2022-09-04 19:06:07
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);
        
        //获得组件
        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);
        
        //添加点击事件
        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;
        }
        
    }

    /**
     * 读手机内存
     */
    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("总内存:"+totalSizeStr + "可用内存:" +alilableSizeStr);
    }

    /**
     * 读SD卡内存
     */
    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("总内存:"+totalSizeStr + "可用内存:" +alilableSizeStr);
    }

    /**
     * 向SD卡写数据
     */
    private void saveFile2Sd()  {
        //获得SD卡的状态
        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, "保存成功", Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            Toast.makeText(this, "SD卡不存在", Toast.LENGTH_SHORT).show();
        }
    }
    
    /**
     * 从SD卡读数据
     */
    private void readFile2Sd()  {
        //获得SD卡的状态
        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, "读取成功", Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                Toast.makeText(this, "SD卡不存在", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(this, "SD卡不存在", Toast.LENGTH_SHORT).show();
        }
    }

}