向sd卡读写数据
/data/data 是本地存储
/storage/ 是外部存储
SD卡存储


<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
public void writeToSdCard() throws IOException {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, permissions, REQUEST_CODE);
} else {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String name = et_of_name.getText().toString();
String content = et_of_content.getText().toString();
String dir = getExternalFilesDir(null).getAbsolutePath();
dir = dir + "/" + name;
FileOutputStream fos = new FileOutputStream(dir);
fos.write(content.getBytes("utf-8"));
fos.close();
Toast.makeText(this, "save to external sdcard", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "without external sdcard", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE:
if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
try {
writeToSdCard();
} catch (IOException e) {
Toast.makeText(this, "failed save to external sdcard", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "Write External SDCARD Permission Not Granted", Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
public void read(View v) throws IOException {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String name = et_of_name.getText().toString();
String dir = getExternalFilesDir(null).getAbsolutePath();
dir = dir + "/" + name + "txt";
InputStream fis = new FileInputStream(dir);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len = -1;
byte[] bytes = new byte[1024];
while ((len = fis.read(bytes)) != -1) {
bos.write(bytes);
}
String content = bos.toString();
et_of_content.setText(content);
} else {
Toast.makeText(this, "without external sdcard", Toast.LENGTH_SHORT).show();
}
}
判断文件夹是否存在
File file = new File(dir);if(!file.exists()) { file.mkdir();}
String dir =
context.getExternalFilesDir(null).getAbsolutePath();
/storage/emulated/0/Android/data/com.example.storage/files
Environment.getExternalStorageDirectory().getAbsolutePath()
/storage/emulated/0
向sd卡读写数据的更多相关文章
- Android中向SD卡读写数据,读SD卡和手机内存
package com.example.sdoperation; import java.io.BufferedReader; import java.io.File; import java.io. ...
- 第36章 SDIO—SD卡读写测试
第36章 SDIO—SD卡读写测试 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/f ...
- 第36章 SDIO—SD卡读写测试—零死角玩转STM32-F429系列
第36章 SDIO—SD卡读写测试 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/f ...
- SD卡读写之FileNotFoundException: /storage/emulated/0object.txt: open failed: ENOENT (No such file or dir
读写sd卡中的文件依照例如以下步骤:1调用Environment的getExternalStorageState()方法推断手机上是否插入了sd卡.而且应用程序具有读写SD卡的能力 //假设手机已经插 ...
- 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(八)-认识内存管理
[STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 [STM3 ...
- 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(七)-准备移植FatFs
[STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 [STM3 ...
- 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(六)-FatFs使用的思路介绍
[STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 [STM3 ...
- 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡
由于一张SD卡要能读写,涉及到的技术有些多,我打算分以下几篇博客 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含 ...
- 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍
其他链接 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 ...
随机推荐
- LeetCode OJ--Merge Sorted Array *
http://oj.leetcode.com/problems/merge-sorted-array/ 两个有序数组A和B的归并排序,将结果存到A中.因为已知两数组长度且A的数组足够大,所以倒着处理, ...
- javascript中实现类似php 的var_dump
javascript语言中的调试功能少得可怜,如果涉及到第三方返回的对象数据更是使得开发程度加大.想到php中的var_dump,print_r简单好用,极大程序上方便了开发工作,在网上乱找一通,终于 ...
- pycharm上传代码到码云错误现象用户密码
>>出现此时错误说明在pycharm>git登录用户名或密码是错误的(必须填成是自己注册的码云邮箱和密码 不允许出现中文)并且无法在当前修改用户名或密码 >>接下来打开电 ...
- Springboot 集成 Thymeleaf 及常见错误
Thymeleaf模板引擎是springboot中默认配置,与freemarker相似,可以完全取代jsp,在springboot中,它的默认路径是src/main/resources/templat ...
- android clipRect Op.xxx各个参数理解
有点小啰嗦的一篇学习笔记,可以直接看最后得出的结论:前面的各种图片和说明都是为最后的结论服务的 1)剪切:和平常画图工具剪切的作用一样,在画布上剪切一个区域,比如剪切一个Rect区域,画布canvas ...
- hadoop常见错误
hadoop常见错误集锦: 1.DataXceiver error processing WRITE_BLOCK operation ERROR org.apache.hadoop.hdfs.serv ...
- 关于JavaScript禁止点击事件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Odoo超售订单
当 交付给客户的货物多于订购的数量时,就形成'超售'状态: 对于超售的部分,需要进行开票处理,以及根据情况修改交货 发生超售的前提是,产品开票策略为 '按订购数量开票' 同时需要 允许 ...
- openERP server action,最强大的功能,没有之一
Jeffery9@gmail.com 出品 @jeffery-陈帆 原理 ations OE定义了ir.actions.actions,并从中派生了众多的子类 ir.actions.client ir ...
- shell(2):正则表达式
一.整理正则表达式博客 (1)正则 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则. 在linux中,通配符是由shel ...