Android——数据存储:手机内部存储



存取字符串和存取图片不相同
xml
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_3"
android:hint="要存储的的内容"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_4"
android:hint="从文件中读取的内容"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="保存"
android:layout_weight="1"
android:onClick="onclick3"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="获取"
android:layout_weight="1"
android:onClick="onclick4"/>
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="保存文件"
android:layout_weight="1"
android:onClick="onclick5"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="获取文件"
android:layout_weight="1"
android:onClick="onclick6"/>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/maps"
android:id="@+id/iv_3"/>
java
//定义文件名
//定义常量,一般全大写
final String FILENAME = "test.txt"; //手机文件存储
public void onclick3(View view)
{
//1.获取要存储的内容
String content = et_3.getText().toString();
//2.获取输出流 以数据为基准 从手机存储往文件走为输出流
try {
//追加模式
FileOutputStream fos_1 = openFileOutput(FILENAME,MODE_APPEND); //3.构造打印流 PrintStream
PrintStream pm = new PrintStream(fos_1); //4.写入内容(换行)
pm.println(content); //5.关闭
pm.close();
fos_1.close(); Toast.makeText(Activitydata.this, "保存成功", Toast.LENGTH_SHORT).show(); } catch (Exception e) {
e.printStackTrace(); Toast.makeText(Activitydata.this, "保存失败", Toast.LENGTH_SHORT).show();
} } //获取
public void onclick4(View view)
{
//1.获取输入流 从文件到手机存储
try {
FileInputStream fis = openFileInput(FILENAME); //2.用数组方法读取
//定义读取的数组
byte[] b = new byte[1024]; //3.读出的数据的长度
int i=0;
StringBuilder sbr = new StringBuilder(); //fis.read(b)返回长度
while((i=fis.read(b))>0)
{
//在这里需要字符串,转为字符串
sbr.append(new String(b,0,i));
} fis.close(); //显示读出的内容
et_4.setText(sbr); Toast.makeText(Activitydata.this, "读取成功", Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(Activitydata.this, "读取失败", Toast.LENGTH_SHORT).show();
} } //操作assets内的文件
public void onclick5(View view)
{
//1.获取AssetManager
AssetManager assetManager = getAssets(); //2.打开文件 返回输入流 把文件读到内存里
try {
InputStream is = assetManager.open("touxiang.jpg"); //3.获取输出流
FileOutputStream fos = openFileOutput("touxiang2.jpg",MODE_PRIVATE); //4.边读边写
byte[] b =new byte[1024]; int i = 0; while((i = is.read(b))>0)
{
fos.write(b, 0, i);
} fos.close();
is.close(); Toast.makeText(Activitydata.this, "保存成功", Toast.LENGTH_SHORT).show(); } catch (IOException e) {
e.printStackTrace(); Toast.makeText(Activitydata.this, "保存失败", Toast.LENGTH_SHORT).show();
} } //读取文件 从手机内部存储读图片文件
public void onclick6(View view)
{ //改变ImageView的图片来源,指向手机存储空间 //1.获取文件存储的绝对路径
String filepath = getFilesDir().getAbsolutePath(); //2.组合完整路径
filepath += "/touxiang2.jpg"; Toast.makeText(Activitydata.this, "path= "+filepath, Toast.LENGTH_SHORT).show(); //3.生成位图实例
Bitmap bm = BitmapFactory.decodeFile(filepath); //4.改变ImageView的图片来源
iv_3.setImageBitmap(bm); }



Android——数据存储:手机内部存储的更多相关文章
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (一) —— 总览
Android数据的四种存储方式SharedPreferences.SQLite.Content Provider和File (一) —— 总览 作为一个完成的应用程序,数据存储操作是必不可少的. ...
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences
除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息.其存储位置在/data ...
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (四) —— ContentProvider
ContentProvider是安卓平台中,在不同应用程序之间实现数据共享的一种机制.一个应用程序如果需要让别的程序可以操作自己的数据,即可采用这种机制.并且此种方式忽略了底层的数据存储实现,Cont ...
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (二) —— SQLite
SQLite是一种转为嵌入式设备设计的轻型数据库,其只有五种数据类型,分别是: NULL: 空值 INTEGER: 整数 REAL: 浮点数 TEXT: 字符串 BLOB: 大数据 在SQLite中, ...
- (转)Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences
除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息.其存储位置在/data ...
- Android数据存储之内部存储、外部存储
首先来介绍下什么是内部存储? 在Android平台下,有着自己独立的数据存储规则,在windows平台下,应用程序能够自由的或者在特定的訪问权限基础上訪问或改动其它应用程序下的文件资源. 可是在And ...
- (转)Android如何编程设置APP安装位置(外部存储或内部存储)?
Beginning with API Level 8, you can allow your application to be installed on the external storage ( ...
- [Android]Android数据的四种存储方式
存储方式 Android提供以下四种存储方式: SharePreference SQLite File ContentProvider Android系统中数据基本都是私有的,一般存放在“data/d ...
- [转][Android]Android数据的四种存储方式
android.database.sqlite类 SQLiteQueryBuilder java.lang.Object android.database.sqlite.SQLiteQueryBuil ...
随机推荐
- DPDK架构与特点
当年在某公司实习的时候,当时老大给了我一份DPDK的文档,说是将来很有用,熟悉DPDK对能力提高也有帮助,就试着翻译了 <Intel DPDK Getting Started Guide> ...
- hadoop编译map/reduce时的问题
参考链接 http://hadoop.apache.org/common/docs/stable/mapred_tutorial.html http://blog.endlesscode.com/20 ...
- TortoiseSVN 清空已保存的用户信息
http://blog.csdn.net/zb358983019/article/details/72898231.如果使用的是安装版的SVN,则打开系统开始菜单中Tortoise下的Settings ...
- python学习笔记——创建事件对象Event
1 Event对象的基本概述 用 multiprocessing.Event 实现线程间通信,使用multiprocessing.Event可以使一个线程等待其他线程的通知,我们把这个Event传递到 ...
- Effective C++:条款33:避免遮掩继承而来的名称
(一) 以下这段代码: int x; void someFunc() { double x; //local variable std::cin>>x; //read a new valu ...
- java的jdbc简单封装
在学了jdbc一段时间后感觉自己写一个简单的封装来试试,于是參考的一些资料就写了一下不是多好,毕竟刚学也不太久 首先写配置文件:直接在src下建立一个db.properties文件然后写上内容 < ...
- FreeSWITCH检测DTMF数据的方法
一.RFC2833 1. 介绍: RFC2833为带内检测方式,通过RTP传输,由特殊的rtpPayloadType即TeleponeEvent来标示RFC2833数据包.同一个DTMF按键通常会对应 ...
- jquery.dataTables的用法
写页面前端时,使用表格的插件可以快速漂亮的排版.本例子中使用jquery.dataTables来处理table.直接来点干货 html代码如下 <table cellpadding=" ...
- ps钢笔工具 (重要工具)
ps钢笔工具 ctrl键 + 鼠标选中操作的点 = 移动点 alt键 + 方点 + 拖拉 = 拖出两个手柄 和 控制两端弧度 alt键 + 圆点 = 控制单向弧度
- scala-trait实现AOP编程
步骤1:声明表示基本动作方法的模块Taction //声明表示基本动作方法的模块Taction trait TAction { def doAction } 步骤2:定义一下加入了前置处理和后置处理的 ...