SD卡外部file存储

1.说明:

>应用运行用到的数据文件(如图片)可以保存到sd卡中

>文件类型: 任意

>数据保存的路径:

--- 路径1:/storage/sdcard/Android/data/packageName/files/

--- 路径2: /storage/sdcard/xxx/

>路径1:其它应用可以访问,应用卸载时删除

>路径2: 其它应用可以访问,应用卸载时不会删除

>必须保证sd卡挂载在手机上才能读写,否则不能操作


2.相关API

1). Environment : 操作SD卡的工具类

> 得到SD卡的状态 : Environment.getExternalStorageState()

得到SD卡的路径 :Environment.getExternalStorageDirectory()

SD卡可读写的挂载状态值 :Environment.MEDIA_MOUNTED

2). context.getExtermalFilesDir():

> 得到 /mnt/sdcard/Android/data/packageName/files/xxx.txt

3). android.permission.WRITE_EXTERNAL_STORAGE

3.相关代码

(代码中为方便用throw,应该用try)

public class OfActivity extends Activity {

	private EditText et_of_name;
	private EditText et_of_content;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_of);
		et_of_name = (EditText) findViewById(R.id.et_of_name);
		et_of_content = (EditText) findViewById(R.id.et_of_content);
	}

	/*
	 * 路径一保存
	 */
	public void save(View v) throws IOException {
		// 1.判断sd卡状态,如果是挂载的状态才继续,否则提示
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			// 2. 读取输入的文件名/内容
			String fileName = et_of_name.getText().toString();
			String content = et_of_content.getText().toString();
			// 3. 得到指定文件的OutputStream
			// 1).得到sd卡下的files路径
			File filesPath = getExternalFilesDir(null);
			// 2).组成完整路径
			String filePath = filesPath + "/" + fileName;
			// 3).创建FileOutputStream
			FileOutputStream fos = new FileOutputStream(filePath);
			// 4.写数据
			fos.write(content.getBytes("utf-8"));
			fos.close();
			// 5.提示
			Toast.makeText(this, "保存完成", 0).show();
		} else {
			Toast.makeText(this, "sd卡没有挂载", 0).show();
		}

	}

	/*
	 * 路径一读取
	 */
	public void read(View v) throws IOException {
		// 1.判断sd卡状态,如果是挂载的状态才继续,否则提示
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			// 2. 读取输入的文件名/内容
			String fileName = et_of_name.getText().toString();
			// 3. 得到指定文件的InputStream
			// 1).得到sd卡下的files路径
			File filesPath = getExternalFilesDir(null);
			// 2).组成完整路径
			String filePath = filesPath + "/" + fileName;
			// 3).创建FileInputStream
			FileInputStream fis = new FileInputStream(filePath);
			// 4.读取数据,成String
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int len;
			while ((len = fis.read(buffer)) != -1) {
				baos.write(buffer, 0, len);
			}
			String content = baos.toString();
			baos.close();
			fis.close();
			// 5.显示
			et_of_content.setText(content);
		} else {
			Toast.makeText(this, "sd卡没有挂载", 0).show();
		}

	}

	/*
	 * 路径二保存
	 */
	public void save2(View v) throws IOException {
		// 1.判断sd卡状态,如果是挂载的状态才继续,否则提示
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			// 2. 读取输入的文件名/内容
			String fileName = et_of_name.getText().toString();
			String content = et_of_content.getText().toString();
			// 3. 得到指定文件的OutputStream
			// 1). /storage/sdcard/
			String sdPath = Environment.getExternalStorageDirectory()
					.getAbsolutePath();
			// 2). /storage/sdcard/atguigu/ (创建文件夹)
			File file = new File(sdPath + "/atguigu");
			if (!file.exists()) {
				file.mkdirs();
			}
			// 3). /storage/sdcard/atguigu/xxx.txt
			String filePath = sdPath + "/atguigu" + fileName;
			// 4).创建输出流
			FileOutputStream fos = new FileOutputStream(filePath);
			// 4.写数据
			fos.write(content.getBytes("utf-8"));
			fos.close();
			// 5.提示
			Toast.makeText(this, "保存完成", 0).show();
		} else {
			Toast.makeText(this, "sd卡没有挂载", 0).show();
		}

	}

	/*
	 * 路径二读取
	 */
	public void read2(View v) throws IOException {
		// 1.判断sd卡状态,如果是挂载的状态才继续,否则提示
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			// 2. 读取输入的文件名/内容
			String fileName = et_of_name.getText().toString();
			// 3. 得到指定文件的InputStream
			String sdPath = Environment.getExternalStorageDirectory()
					.getAbsolutePath();
			String filePath = sdPath + "/atguigu" + fileName;
			FileInputStream fis = new FileInputStream(filePath);
			// 4.读取数据,成String
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int len;
			while ((len = fis.read(buffer)) != -1) {
				baos.write(buffer, 0, len);
			}
			String content = baos.toString();
			baos.close();
			fis.close();
			// 5.显示
			et_of_content.setText(content);
		} else {
			Toast.makeText(this, "sd卡没有挂载", 0).show();
		}

	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值