Android-SD卡相关操作
SD卡相关操作
1、获取 App 文件目录
//获取 当前APP 文件路径
String path1 = this.getFilesDir().getPath();
当前APP目录也就是应用的这个目录 /data/data/com.tiger.helloworld/files

2、获取外部存储器 路径
一般手机文件管理 根路径 /storage/emulated/0/
//获取外部存储器 路径
String path2= Environment.getExternalStorageDirectory().getPath();
3、判断SD卡是否可用
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
4、获取SD总大小,可用空间
// 获取SD卡的总大小,可用空间
File file= Environment.getExternalStorageDirectory();
long totalSpace= file.getTotalSpace(); //总大小
long usableSpace= file.getUsableSpace(); //可用空间
//转换数据格式
String formatTotalSpace= Formatter.formatFileSize(MainActivity.this,totalSpace);
String formatUsableSpace=Formatter.formatFileSize(MainActivity.this,usableSpace);
Toast.makeText(MainActivity.this, "Total Space:"+formatTotalSpace
+" UsableSpace:"+formatUsableSpace, Toast.LENGTH_LONG).show();
5.文本文件读写
Type1:
1、写入数据
try {
String result = userName + "_" + userPwd;
//【1】创建一个File 类 指定我们要把数据存储的位置
File file = new File("/data/data/com.tiger.helloworld/info.txt");
//【2】 创建一个文件输出流
FileOutputStream fos = new FileOutputStream(file);
//【3】写入数据
fos.write(result.getBytes());
fos.close();
return true;
} catch (Exception e) {
Log.e("Utils", e.getMessage());
e.printStackTrace();
return false;
}
2、读取数据
try {
File file = new File("/data/data/com.tiger.helloworld/info.txt");
FileInputStream fis = new FileInputStream(file);
BufferedReader bufr = new BufferedReader(new InputStreamReader(fis));
String userInfo = bufr.readLine();
fis.close();
return userInfo;
} catch (Exception e) {
Log.e("Utils", e.getMessage());
e.printStackTrace();
return null;
}
Type2:
1、通过 Content 写入数据
try {
String result = userName + "_" + userPwd;
FileOutputStream fos = context.openFileOutput("info.txt", context.MODE_PRIVATE);
fos.write(result.getBytes());
fos.close();
return true;
} catch (Exception e) {
Log.e("Utils", e.getMessage());
e.printStackTrace();
return false;
}
2、通过 Content 读取数据
try {
FileInputStream fis = context.openFileInput("info.txt");
BufferedReader bufr = new BufferedReader(new InputStreamReader(fis));
String userInfo = bufr.readLine();
/*while (bufr.readLine()!=null){
str+=bufr.readLine();
}*/
fis.close();
return userInfo;
} catch (Exception e) {
Log.e("Utils", e.getMessage());
e.printStackTrace();
return null;
}
6.保存InputStream图片到本地相册中
/**
* 保存图片,并显示到 Gallery 中
*/
public void saveImage(InputStream inputStream){
try {
File externalFile = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File directoryFile = new File(externalFile.getPath() + "/MY");
//若目录不存在则创建
if (!directoryFile.exists()) {
directoryFile.mkdirs();
}
File imageFile = new File(directoryFile, "qr"+directoryFile.list().length+".jpg");
FileOutputStream fos=new FileOutputStream(imageFile);
byte[] buffer=new byte[1024];
int len=-1;
while ((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);
}
inputStream.close();
fos.close();
//发送广播扫面文件,使图片显示在Gallery中
Intent intent=new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(imageFile));
mContext.sendBroadcast(intent);
Log.e("","Save successful!");
} catch (Exception e) {
e.printStackTrace();
Log.e("","Save unsuccessful!");
}
}
Android-SD卡相关操作的更多相关文章
- Android入门开发之SD卡读写操作(转)
SD卡的读写是我们在开发android 应用程序过程中最常见的操作.下面介绍SD卡的读写操作方式: 1. 获取SD卡的根目录 String sdCardRoot = Environment.getE ...
- Android SD卡存储
原创文章,转载请注明出处:http://www.cnblogs.com/baipengzhan/p/Android_SDcard_store.html 一 概念 SD卡存储空间比较大,当需要存取较大的 ...
- Android SD卡创建文件和文件夹失败
原文:Android SD卡创建文件和文件夹失败 功能需要,尝试在本地sd卡上创建文件和文件夹的时候,报错,程序崩溃. 一般情况下,是忘记给予sd卡的读写权限.但是这里面权限已经给了,还是报错. 在网 ...
- Android——SD卡工具类——SDCardUtils.java
package com.xdsjs.save.utils; /** * SD卡相关的辅助类 * Created by xdsjs on 2015/10/13. */ import java.io.Fi ...
- [笔记]SD卡相关资料
ESD静电放电模块 我知道的flash分为两种NOR flash和NAND flash,NOR falsh容量一般为1~16M用于单片机代码存储,NAND flash最小的是8M最大的现在听说有90G ...
- imx6 android SD卡启动
工作中需要将imx6的android系统从SD卡启动,所以就分析了MfgTool中的脚本,分析android的分区情况,并尝试自己操作,竟然成功了,记录于此. 参考文档 http://www.kanc ...
- Android SD卡读写
package com.jredu.zuiyou.activity; import android.os.Bundle;import android.os.Environment;import and ...
- FPGA之SPI SD卡读操作
这几天在FPGA调试与SD通信,读SD卡里的图片,之前接触32时没有去研究过SD卡,不太熟悉操作流程,在网上找了很多资料,也看了几个32开发板的资料,但大多数都讲得不是特别清楚,只能瞎操作了一番,在别 ...
- Android SD卡上文件
1. 得到存储设备的目录:/SDCARD(一般情况下) SDPATH=Environment.getExternalStorageDirectory()+"/"; 2. 判断SD卡 ...
随机推荐
- [bilibili]弹幕屏蔽列表
<filters> <item enabled="true">t=定单身</item> <item enabled="true& ...
- nginx日志相关的查询
IP相关统计 统计IP访问量(独立ip访问数量) awk '{print $1}' access.log | sort -n | uniq | wc -l 查看某一时间段的IP访问量(4-5点) gr ...
- CentOS配置防火墙操作实例
CentOS 配置防火墙操作实例(启.停.开.闭端口): 注:防火墙的基本操作命令: 查询防火墙状态: [root@localhost ~]# service iptables status<回 ...
- Java入门:基础算法之二进制转换为十进制
Java有两种方法可以将二进制数转换为十进制数: 1)使用Integer类的Integer.parseInt()方法. 2)自己编写转换逻辑. 方法1:使用Integer.parseInt()实现二进 ...
- elk插件以及分词器安装
ElasticSearch-Head 安装配置因为安装 ElasticSearch-Head 需要使用到 npm 包管理器,所以需要我们提前安装好 NodeJS ,安装 NodeJS 的方法可以参考: ...
- Numpy 系列(七)- 常用函数
在了解了 Numpy 的基本运算操作,下面来看下 Numpy常用的函数. 数学运算函数 add(x1,x2 [,out]) 按元素添加参数,等效于 x1 + x2 subtract(x1,x2 ...
- Nginx反向代理、负载均衡、动静分离、缓存、压缩、防盗链、跨域访问
一.反向代理 1.在192.168.189.130机器启动tomcat服务,http://192.168.189.130:8080/ 访问服务正常 2.在192.168.189.131机器配置ngin ...
- MySQL学习笔记(五)并发时经典常见的死锁原因及解决方法
MySQL都有什么锁? MySQL有三种锁的级别:页级.表级.行级. 表级锁:开销小,加锁快:不会出现死锁:锁定粒度大,发生锁冲突的概率最高,并发度最低. 行级锁:开销大,加锁慢:会出现死锁:锁定粒度 ...
- 《我是一只IT小小鸟读后感》
在我步入大学前,并未了解何为IT,真是毫无知晓.由于种种原因最终还是选择了软件工程专业,是 对是错,是福是祸,不知该不该去考虑,但即已然 选择了这条路,便得付出一些努力,这个世界总 是有许多在默默努力 ...
- spring事务源码分析结合mybatis源码(一)
最近想提升,苦逼程序猿,想了想还是拿最熟悉,之前也一直想看但没看的spring源码来看吧,正好最近在弄事务这部分的东西,就看了下,同时写下随笔记录下,以备后查. spring tx源码分析 这里只分析 ...