向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总线,命令的相关介绍 ...
随机推荐
- 安装sass的艰难过程
1.ruby安装 因为sass依赖于ruby环境,所以装sass之前先确认装了ruby.先导官网下载个ruby 在安装的时候,请勾选Add Ruby executables to your PATH这 ...
- inotify+rsync的初步配置
inotify的简单配置 Linux内核从2.6.13开始,引入了inotify机制.通过intofity机制,能够对文件系统的变化进行监控,如对文件进行创建.删除.修改等操作,可以及时通知应用程序 ...
- 洛谷—— P1875 佳佳的魔法药水
https://www.luogu.org/problemnew/show/1875 题目背景 发完了 k 张照片,佳佳却得到了一个坏消息:他的 MM 得病了!佳佳和大家一样焦急 万分!治好 MM 的 ...
- HDU 1011 Starship Troopers (树dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 题意: 题目大意是有n个房间组成一棵树,你有m个士兵,从1号房间开始让士兵向相邻的房间出发,每个 ...
- Java 获取当前时间及实现时间倒计时功能
引言 在一些项目中或是一些特殊的业务场景中,需要用到显示系统的当前时间,以及一些固定的时间倒计时,时间到后做一些什么事情的业务 .接下来咱们就具体看看代码是怎么实现的: <%@ page lan ...
- Java文件夹操作,判断多级路径是否存在,不存在就创建(包括windows和linux下的路径字符分析),兼容Windows和Linux
兼容windows和linux. 分析: 在windows下路径有以下表示方式: (标准)D:\test\1.txt (不标准,参考linux)D:/test/1.txt 然后在java中,尤其使用F ...
- Maven的构建生命周期理解
以下引用官方的生命周期解释https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html: 一.构建生命 ...
- DELPHI 10.2(TOKYO) FOR LINUX的兼容性说明
DELPHI 10.2(TOKYO) FOR LINUX的兼容性说明 自DELPHI 10.2(TOKYO) 始开始支持Linux . Delphi Linux 编译器 64 位 Linux 平台支持 ...
- iOS应用崩溃日志揭秘
这篇文章还可以在这里找到 英语 Learn how to make sense of crash logs! 本文作者是 Soheil Moayedi Azarpour, 他是一名独立iOS开发者. ...
- 【前台 ajax】前台ajax请求,地址正确,但是报错不进入后台
前台ajax请求,地址正确,但是报错不进入后台 出现上述问题,可能的情况是 1.ajax用的post,而后台限定用get,或者所有的post请求都被拦截,所以不能正常进入并且报错403 @Reques ...