首页 > 代码库 > Android中向SD卡读写数据,读SD卡和手机内存
Android中向SD卡读写数据,读SD卡和手机内存
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(); } }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。