向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总线,命令的相关介绍 ...
随机推荐
- es6 Number.isFinite()、Number.isNaN()、Number.isInteger()、Math.trunc()、Math.sign()、Math.cbrt()、Math.fround()、Math.hypot()、Math 对数方法
ES6在Number对象上,新提供了Number.isFinite()和Number.isNaN()两个方法,用来检查Infinite和NaN这两个特殊值. Number.isFinite()用来检查 ...
- 输入框为数字类型时防止maxlength属性不起作用
<input type="number" oninput="if(value.length>5)value=value.slice(0,5)" /& ...
- Java实验--关于课上找“水王”问题分析
问题的表述就是说有那么一个人,他在一个论坛上发帖,然后每贴必回,自己也发帖.那么这个人在发帖的数目上就超过了整个论坛的帖子数目的一半以上. 我对这个问题一开始的思路是,用SQL语句获取整个列表中的数据 ...
- andriod 获得应用程序名称
import android.annotation.TargetApi; import android.app.Activity; import android.app.AlertDialog; im ...
- [反汇编练习] 160个CrackMe之028
[反汇编练习] 160个CrackMe之028. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...
- Python学习笔记8:标准库之正則表達式
Python拥有强大的标准库.从如今起,開始学习标准库中提供的一些经常使用功能. 首先看正則表達式(regular expression),它的主要功能是从字符串(string)中通过特定的模式(pa ...
- 江湖问题研究-- intent传递有没有限制大小,是多少?
出门一步,便是江湖.江湖上有很多流言. 比方这条: intent传递是有限制大小的,详细在40KB左右. 当然也有传言说是1M左右. 数百头母驴为何半夜慘叫? 小卖部安全套为何屡遭黑手? 女生宿舍内裤 ...
- C++11 并发指南三(std::mutex 详解)(转)
转自:http://www.cnblogs.com/haippy/p/3237213.html 上一篇<C++11 并发指南二(std::thread 详解)>中主要讲到了 std::th ...
- ffmpeg 错误 real-time buffer [USB2.0 Camera] [video input] too full or near too full (101% of size: 30412)
利用ffmpeg 获取USB 或者本地摄像机视频,并将视频编码后保存本地文件或者发送到远端流媒体服务经常会出现 类似real-time buffer [USB2.0 Camera] [video in ...
- web前端面试系列 - 算法( 数组去重 )
1. 思路:设置一个临时数组temp,然后遍历要去重的数组arr,如果arr中的元素能够在temp中找到,则跳过此元素,否则将此元素存入temp,最后返回temp. 实现一 function uniq ...