Android数据存储——SDcard

这里说的sd卡存储是指的是手机自身内存,存储方法跟缓存中存储是一样的,只是获取存储位置的绝对路径的方法有所变化,该存储方法存储的位置在/mnt/sdcard/
在XML文件中定义一个输入框和一个按钮

  <EditText
        android:id="@+id/edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="写入数据"/>
 <Button
        android:id="@+id/button_write_sdcard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="将数据写入SD卡"/>

在MainActivity中添加点击事件,将从EditText中获取的数据写入到指定位置

private void writeSDcard(){
        File file=new File(Environment.getExternalStorageDirectory(),"SDstorige");
        if (!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            FileOutputStream is=new FileOutputStream(file);
            BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(is));
            bw.write(mEditText.getText().toString());
            bw.flush();
            bw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

可以看到,只是获取文件路径的方法不同而已。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值