Android 数据存储(尚学堂)

1.SharedPreferences 存储

2.手机内部file存储

3.手机外部file存储

4.sqlite数据存储

5.远程服务器存储

SharedPreferences 存储

SP 存储专门存储一些单一的小数据

存储数据的类型:boolean,float,int,long,String

数据保存的路径:/data/data/packageName/shared_prefs/xxx.xml

可以设置数据只能

 1). 位置

          /data/data/packageName/shared_prefs/xxx.xml

     2). 特点

          a. 小数据 key--value

          b. 应用卸载时会自动删除此数据

     3). 相关API

如何在Android查看保存文件 .xml

 

 

package com.example.testapplication;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;

public class SpActivity extends ApplicationActivity{
    private EditText et_sp_key;
    private EditText et_sp_value;

    private SharedPreferences sp;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sp);

        et_sp_key = (EditText) findViewById(R.id.et_sp_key);
        et_sp_value = (EditText) findViewById(R.id.et_sp_value);

        // 第一步 得到sp对象 atguigu.xml
        sp = getSharedPreferences("atguigu", Context.MODE_PRIVATE);


    }

    public void save(View view){
        // 第二步的到editor对象
        Editor editor = sp.edit();
        // 3.得到输入的key/value
        String key = et_sp_key.getText().toString();
        String value = et_sp_value.getText().toString();
        // 4.使用editor保存key-value
        editor.putString(key,value).commit();
        //5.提示
        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();

    }

    public void read(View view){
        // 1.得到输入key
        String key = et_sp_key.getText().toString();
        //2.根据key读取对应的value
        String value = sp.getString(key,null);
        // 3. 显示
        if(value == null){
            Toast.makeText(this, "没有找到对应的value", Toast.LENGTH_SHORT).show();
        }else{
            et_sp_value.setText(value);
        }


    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/et_sp_key"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="存储的key" />

    <EditText
        android:id="@+id/et_sp_value"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        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:layout_weight="1"
            android:onClick="save"
            android:text="保 存" />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="read"
            android:text="读 取" />
    </LinearLayout>

</LinearLayout>

 手机内部文件存储

应用运行需要一些较大的数据或图片可以用文件保存的手机内部

文件类型:任意

  数据保存路径:/data/data/packageName/files/文件

应用卸载时会自动删除此数据

 

 

package com.example.testapplication;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class IFActivity extends ApplicationActivity{
    private ImageView iv_if;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_if);

        iv_if = (ImageView) findViewById(R.id.iv_if);

    }
    public void save(View view) throws IOException {
        // 1.得到InputStream --> 读取assets下的logo.png
         // 得到AssetManger
        AssetManager manager = getAssets();
        // 读取文件
        InputStream is = manager.open("logo.png");

        // 2.得到OutputStream --> /data/data/packageName/files/logo.png
        FileOutputStream fos = openFileOutput("logo.png", Context.MODE_PRIVATE);
        // 3.边读边写
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = is.read(buffer)) != -1){
            fos.write(buffer,0,len);
        }
        fos.close();
        is.close();
        // 4. 提示
        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
    }

    public void read(View view){
        //1. 得到图片文件的路径
        // /data/data/packageName/files
        String filesPath = getFilesDir().getAbsolutePath();
        String imagePath = filesPath+"/logo.png";
        //2. 读取加载图片文件得到bitmap对象
        Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
        //3. 将其设置到imageView中显示
        iv_if.setImageBitmap(bitmap);

    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="1. 将asserts下的logo.png保存到手机内部\n2. 读取手机内部图片文件显示"
        android:textColor="#ff0000"
        android:textSize="15sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/btn_if_save"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="save"
            android:text="保 存" />

        <Button
            android:id="@+id/btn_if_read"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="read"
            android:text="读 取" />
    </LinearLayout>

    <ImageView
        android:id="@+id/iv_if"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" />

</LinearLayout>

手机外部文件存储SD

 

 <!--操作sd卡--> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Guess you like

Origin blog.csdn.net/weixin_38107457/article/details/121734332