android-数据存储之手机内部file存储
一、基础概要
1、说明:
1>应用程序运行需要一些较大的数据或者图片可保存在手机内部
2>文件类型:任意
3>路径:/data/data/packageName/files/
4>卸载应用时会删除此数据文件
5>也可以设置操作数据文件的权限(同SharedPreferences)
二、练习
1>FileInputStream fis=openFileInput("logo.png"); 读取文件
2>FileOutputStream fos=openFileOutput("logo.png",MODE_PRIVATE); 保存文件
3>File filesDir=getFilesDir(); 得到files文件夹对象
4>操作asserts下的文件:
.context.getAssets() 得到AssetManager
.InputStream open(filename); 读取文件
5>加载图片文件:
Bitmap bitmap=BitmapFactory.decodeFile(String pathName); (Drawable:表示可绘制图片对象)
保存图文件:
1>得到InputStream :读取assets下的logo.png
AssetManager manager=getAssets();
2>读取文件
InputStream is=manager.open("logo.png");
3>得到OutputStream : /data/data/packageName/files/logo.png
FileOutputStream fos=openFileOutput("logo.png",Context.MODE_PRIVATE);
4>边读边写:
byte[] buffer=new byte[1024];
int len=-1;
while((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
fos.close();
is.close();
读取图片:
1>得到图片路径: /data/data/packageName/files
String filesPath=getFileDir().getAbsolutePath();
String imgPath=filesPath+"/logo.png";
2>加载图片文件得到bitmap对象:
Bitmap bitmap=BitmapFactory.decodeFile(imgPath);
3>将其设置到imageView中显示:
iv_if.setImageBitmap(bitmap);
三、源代码
保存图片:
AssetManager manager=getAssets();
InputStream is=manager.open("logo.png");
FileOutputStream fos= openFileOutput("logo.png",Context.MODE_PRIVATE);
byte[] buffer=new byte[1024];
int len=-1;
while((len=is.read(buffer))!=-1){
fos.write(buffer, 0, len);
}
fos.close();
is.close();
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();

读取图片:

android-数据存储之手机内部file存储的更多相关文章
- Android——数据存储(课堂代码整理:SharedPreferences存储和手机内部文件存储)
layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...
- Android——数据存储:手机内部存储
存取字符串和存取图片不相同 xml <EditText android:layout_width="match_parent" android:layout_height=& ...
- andorid 数据储存、SharedPreferences存储和手机内部储存
.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...
- Android——手机内部文件存储(作业)
作业:把用户的注册信息存储到文件里,登录成功后读出并显示出来 activity_practice2的layout文件: <?xml version="1.0" encodin ...
- Android开发学习——android数据存储
Android的存储 Android中的数据存储方式及其存储位置 SharedPrefrence存储 1). 位置 /data/data/packageName/shared_pr ...
- (转)获取android手机内部存储空间和外部存储空间的参数 && 如何决定一个apk的安装位置
转:http://blog.csdn.net/zhandoushi1982/article/details/8560233 获取android文件系统的信息,需要Environment类和StatFs ...
- Android数据存储之内部存储、外部存储
首先来介绍下什么是内部存储? 在Android平台下,有着自己独立的数据存储规则,在windows平台下,应用程序能够自由的或者在特定的訪问权限基础上訪问或改动其它应用程序下的文件资源. 可是在And ...
- Android数据存储三剑客——SharedPreferences、File、SQLite
Android中常用的数据存储一般有三种方式:SharedPreferences.文件和SQLite数据库,用来保存需要长时间保存的数据.本文将通过几个具体的小实例来讲解这三种方式的具体实现. 数据存 ...
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (一) —— 总览
Android数据的四种存储方式SharedPreferences.SQLite.Content Provider和File (一) —— 总览 作为一个完成的应用程序,数据存储操作是必不可少的. ...
随机推荐
- 报错:1130-host ... is not allowed to connect to this MySql server 开放mysql远程连接 不使用localhost
执行如下命令报错 mysql -uroot -h${hostIp} -p Enter password:********* ERROR (HY000): Host '$hostIp' is not a ...
- waterMarkTextBox
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...
- Eclipse的详细安装步骤
第一种:这个方法是在线安装的 第二种:下载完整免安装包 首先打开网址:http://www.eclipse.org/ 然后在这里我就选择64位的安装,就以安装安卓开发的举例: 然后下载即可:
- unable to access android sdk add-on list
在bin\properties里添加disable.android.first.run=true
- 智能车学习(一)—— 硬件准备
一.硬件环境准备 1.烧写器焊接 (1)原理图: (2)焊接成品图 2.电源模块: 3.屏幕+键盘 4.单片机 二.软件环境准备 1.安装air6.7 2.安装JLINK驱动 3.安装软件包寄存在GI ...
- HTML的格式、内容容器、表格标签
HTML(Hyper Text Markup Language,超文本标记语言)超指的是超链接. <html> --开始标签 <head> 网页上的控制信息 <ti ...
- 分布式服务框架 Zookeeper -- 管理分布式环境中的数据
转自:http://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/index.html Zookeeper 分布式服务框架是 Apa ...
- 通过maven下载jar包
准备 你得先安装Maven如果不会点击后面的连接查看一二步.http://www.cnblogs.com/luoruiyuan/p/5776558.html 1.在任意目录下创建一个文件夹,其下创建一 ...
- bpl 包的编写和引用
转载:http://www.cnblogs.com/gxch/archive/2011/04/23/bpl.html 为什么要使用包? 答案很简单:因为包的功能强大.设计期包(design-time ...
- 获取datable中某行某列的数据
假设该DataTable有5行和两个字段“Name”,“Phone”, 我如何访问第3行的“Phone”字段的值. DataTable.Rows[2][1].ToString() DataTable. ...