andorid 数据储存、SharedPreferences存储和手机内部储存

时间:2022-06-22 11:37:38

.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hanqi.application3.DataActivity1"
android:orientation="vertical"> <EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_1"
android:hint="Key"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_2"
android:hint="Value"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="保存"
android:layout_weight="1"
android:onClick="bt1_onClick"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="读取"
android:layout_weight="1"
android:onClick="bt2_onClick"/> </LinearLayout>
<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">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="保存"
android:layout_weight="1"
android:onClick="bt3_onClick"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="读取"
android:layout_weight="1"
android:onClick="bt4_onClick"/> </LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="保存文件"
android:layout_weight="1"
android:onClick="bt5_onClick"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="读取文件"
android:layout_weight="1"
android:onClick="bt6_onClick"/> </LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/da1"
android:id="@+id/iv_4"/> </LinearLayout>

.java

package com.hanqi.application3;

import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintStream; public class DataActivity1 extends AppCompatActivity {
EditText et1;
EditText et2;
EditText et3;
EditText et4; ImageView iv4;
SharedPreferences sp; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data1); et1=(EditText)findViewById(R.id.et_1);
et2=(EditText)findViewById(R.id.et_2); et3=(EditText)findViewById(R.id.et_3);
et4=(EditText)findViewById(R.id.et_4);
iv4= (ImageView)findViewById(R.id.iv_4);
//1.获取sp的实例,制定了文件名和操作模式 MODE_PRIVATE私有
sp = getSharedPreferences("mydata",MODE_PRIVATE); }
//操作assets内的文件
public void bt5_onClick(View v)
{
//1.获取AssetManager
AssetManager am = getAssets(); try{
//2.打开文件,获取输入流
InputStream is = am.open("denglu.jpg");
//3.获取输出流
FileOutputStream fos2 = openFileOutput("denglu2.jpg",MODE_PRIVATE);
//4.边度编写
byte[] bb2 = new byte[1024]; int ii2 = 0;
while ((ii2 = is.read(bb2))>0)
{
fos2.write(bb2,0,ii2);
} fos2.close(); is.close(); Toast.makeText(DataActivity1.this, "保存成功", Toast.LENGTH_SHORT).show(); }
catch (Exception e)
{
e.printStackTrace();
Toast.makeText(DataActivity1.this, "保存失败", Toast.LENGTH_SHORT).show();
}
}
//从手机内部存储读图片文件
public void bt6_onClick(View v)
{
//改变ImageView的图片来源,指向手机存储空间 //1.获取文件存储的绝对路径
String filepath = getFilesDir().getAbsolutePath();
//2.组合完整路径
filepath += "/denglu2.jpg";
//3.生成位图实例
Bitmap bm2 = BitmapFactory.decodeFile(filepath);
//4.改变ImageView的图片来源
iv4.setImageBitmap(bm2); }
//文件名
final String FILENAME = "test.txt"; public void bt3_onClick(View v)
{
//1.获取要存储的内容
String content = et3.getText().toString();
//2.获取输出流
try {
FileOutputStream fos1 = openFileOutput(FILENAME, MODE_APPEND);
//3.构造一个打印输出流
PrintStream pm1 = new PrintStream(fos1);
//4.写入内容
pm1.println(content); pm1.close(); fos1.close(); Toast.makeText(DataActivity1.this, "保存成功", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace(); Toast.makeText(DataActivity1.this, "保存失败", Toast.LENGTH_SHORT).show();
} } public void bt4_onClick(View v)
{
try {
//1.获取输入流
FileInputStream fis1 = openFileInput(FILENAME);
//2.定义读取的数组
byte[] bt11 = new byte[1024];
//3.读出数据的长度 int ii = 0; StringBuilder bb1 = new StringBuilder ();
while ((ii = fis1.read(bt11))>0)
{
bb1.append(new String(bt11,0, ii));
} fis1.close();
//设置显示读出的内容
et4.setText(bb1); Toast.makeText(DataActivity1.this, "显示成功", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
e.printStackTrace(); Toast.makeText(DataActivity1.this, "保存失败", Toast.LENGTH_SHORT).show(); } } //保存
public void bt1_onClick(View v)
{
//1.获取Key和Value
String key = et1.getText().toString();
String value = et2.getText().toString();
if (key.length() ==0||value.length() == 0)
{
Toast.makeText(DataActivity1.this, "key或value不能为空", Toast.LENGTH_SHORT).show();
}
else { //2.取得Editor edit编辑器
SharedPreferences.Editor editor = sp.edit();
//3.放入键值对
editor.putString(key,value);
//4.提交保存
boolean b = editor.commit();
if (b)
{
Toast.makeText(DataActivity1.this, "保存成功", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(DataActivity1.this, "保存失败", Toast.LENGTH_SHORT).show();
}
} }
//读取
public void bt2_onClick(View v)
{ //1.获取要读的key
String key = et1.getText().toString();
//2.读并设置文本框
et2.setText(sp.getString(key,"没有发现key")); } }