对sdcard的一些操作

获取容量大小以及将文件保存在sdcard中。

获取手机内存的大小:

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//获取SD卡
		File path = Environment.getExternalStorageDirectory();
		//获取手机内存
		File data = Environment.getDataDirectory();
		long [] sd = getInfo(path);
		long [] phone = getInfo(data);
		
		TextView t = (TextView) findViewById(R.id.total);
		TextView a = (TextView) findViewById(R.id.avilable);
		t.setText(formatString(sd[0]));
		a.setText(formatString(sd[1]));
		
		t = (TextView) findViewById(R.id.phone_total);
		a = (TextView) findViewById(R.id.phone_avilable);
		t.setText(formatString(phone[0]));
		a.setText(formatString(phone[1]));
		
		t = (TextView) findViewById(R.id.total_total);
		a = (TextView) findViewById(R.id.totla_avilable);
		t.setText(formatString(sd[0]+phone[0]));
		a.setText(formatString(sd[1]+phone[1]));
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	
	@SuppressWarnings("deprecation")
	private long[] getInfo(File file) {
		// 关于SD卡的一些信息的对象
		StatFs stat = new StatFs(file.getPath());
		// 获取每个区块的大小
		long blockSize = stat.getBlockSize();
		// 获取区块总数
		long count = stat.getBlockCount();
		// 获取可用区块数量
		long available = stat.getAvailableBlocks();
		return new long[]{blockSize * count,blockSize * available};
	}
	
	private String formatString(long num) {
		return Formatter.formatFileSize(this, num);
	}
}

向sdcard中写入数据

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.os.Environment;
import android.widget.Toast;

public class LoginService {

	/**
	 * 保存用户名和密码
	 * @param context 上下文
	 * @param unserName 用户名
	 * @param password 密码
	 * @return true 保存成功  false 保存失败
	 */
	public static boolean saveUserInfo(Context context ,String userName ,String password){
		//检查SD卡的状态
		String state = Environment.getExternalStorageState();
		if (!Environment.MEDIA_MOUNTED.equals(state)) {
			Toast.makeText(context, "SD卡不可用,请检查SD卡状态", Toast.LENGTH_LONG).show();
			return false;
		}
		//返回文件目录
		File file = new File(Environment.getExternalStorageDirectory(),"info.txt");
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
			fos.write((userName+"##"+password).getBytes());
			fos.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 获取保存的数据
	 * @param ctx
	 * @return
	 */
	public static Map<String,String> getUserInfo(Context ctx){
		File file = new File(Environment.getExternalStorageDirectory(),"info.txt");
		FileInputStream fis = null;;
		try {
			fis = new FileInputStream(file);
			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
			String str = br.readLine();
			String [] info = str.split("##");
			Map<String,String> map = new HashMap<String,String>();
			map.put("userName", info[0]);
			map.put("password", info[1]);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}

注意:

1、向sdcard中写入数据一定要在manifest中的Permission里给予相应权限。因为4.0一下版本默认有读取权限,但无写入权限。4.0以上如果用户设置的话甚至连读取权限都没有。

2、在eclipse中DDMS里查看sdcard中的内容,视频中直接找到sdcard即可,但我的是需要在mnt/sdcard中才能看。不知道是什么问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值