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 ...
随机推荐
- springmvc编码问题
web.xml中加入 <filter> <filter-name>encodingFilter</filter-name> <filter-class> ...
- MyEclipse 8.6插件下载
(源自网络:http://hi.baidu.com/%D4%B5%BA%A3%C7%E9%C9%EE/blog/item/ad86323d1e80a5e33d6d97c6.html 和 http:/ ...
- scanf/sscanf %[]格式控制串的用法(转)
scanf/sscanf %[]格式控制串的用法 scanf中一种很少见但很有用的转换字符:[...]和[ ^...]. #include<stdio.h> int main() { ch ...
- Android开发学习之浅谈显示Intent和隐式Intent
Intent寻找目标组件的两种方式: 显式Intent:通过指定Intent组件名称来实现的,它一般用在知道目标组件名称的前提下,一般是在相同的应用程序内部实现的. 隐式Intent:通过Intent ...
- JMeter学习笔记--使用URL回写来处理用户会话
如果测试的Web应用系统使用URL回写而非Cookie来保存会话信息,那么测试人员需要做一些额外的工作来测试web站点 为了正确回应URL回写,JMeter需要解析从服务器收到的HTML,并得到唯一的 ...
- Access数据库的连接字符串
<!-- Access2007 --> Provider=Microsoft.ACE.OLEDB.12.0;Data Source=data\myAccess_db.accdb;Persi ...
- PLSQL_动态语句的解析(概念)
2014-06-02 Created By BaoXinjian
- [转] 禁止ultraedit自动检查更新的方法
菜单栏: 高级->配置->应用程序布局->其他 取消勾选“自动检查更新” (原文地址:http://blog.csdn.net/redeyerabbit/article/deta ...
- django1.8读书笔记模型高级进阶
一.访问外键和多对多值 例如:模型类定义如下 from django.db import models class Publisher(models.Model): name = models.Cha ...
- SpringMVC中的Model和ModelAndView的区别
1.主要区别 Model是每次请求中都存在的默认参数,利用其addAttribute()方法即可将服务器的值传递到jsp页面中:ModelAndView包含model和view两部分,使用时需要自己实 ...