首页 > 系统 > Android > 正文

Android数据存储方式——SharedPerference

2019-11-06 09:56:21
字体:
来源:转载
供稿:网友
package com.work.datasave;import android.Manifest;import android.content.Context;import android.content.SharedPReferences;import android.content.pm.PackageManager;import android.os.Environment;import android.support.annotation.NonNull;import android.support.v4.app.ActivityCompat;import android.support.v4.content.ContextCompat;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText et_save; private Button btn_save, btn_read; private TextView tv_read; private String name="text.txt"; private String sp_name="tset_sp"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); intiView(); } private void intiView() { et_save = (EditText) findViewById(R.id.et_save); btn_save = (Button) findViewById(R.id.btn_save); btn_read = (Button) findViewById(R.id.btn_read); tv_read = (TextView) findViewById(R.id.tv_read); btn_save.setOnClickListener(this); btn_read.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_save:// saveByInner();// writeBySD();// checkPermission(); saveBySP(); break; case R.id.btn_read:// readByInner();// readBySD(); readBySP(); break; } } //保存数据的方法 public void saveByInner(){ String content=et_save.getText().toString(); try { FileOutputStream out=openFileOutput(name, Context.MODE_PRIVATE);//以覆盖方式保存数据 out.write(content.getBytes()); out.close(); Toast.makeText(this,"已保存",Toast.LENGTH_SHORT).show(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } //读取数据的方法 public void readByInner(){ try { FileInputStream input= openFileInput(name); byte[] buffer= new byte[1024]; int length=0; StringBuffer sb=new StringBuffer(); while ((length=input.read(buffer))!=-1){ sb.append(new String(buffer,0,length)); } input.close(); tv_read.setText(sb.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } //sd卡保存数据的方法 private void writeBySD(){ //先给sdk卡添加读写权限,判断内存卡的状态 if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){//如果内存卡没有挂起 Toast.makeText(this,"内存卡未找到!",Toast.LENGTH_SHORT).show(); return; }else {//如果内存卡已经挂起可以找到,就找到根目录 String path=Environment.getExternalStorageDirectory()+ File.separator+name; File file=new File(path); try { FileOutputStream write=new FileOutputStream(file); String content=et_save.getText().toString(); write.write(content.getBytes()); write.close(); Toast.makeText(this,"SD卡存储成功!",Toast.LENGTH_SHORT).show(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } //sd卡读取的方法 private void readBySD(){ //判断内存卡状态 if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ Toast.makeText(this,"内存卡未找到!",Toast.LENGTH_SHORT).show(); return; }else { String path=Environment.getExternalStorageDirectory()+File.separator+name; File file=new File(path); try { FileInputStream read=new FileInputStream(file); int length=0; StringBuffer sb=new StringBuffer(); byte[] buffer=new byte[1024]; while ((length=read.read(buffer))!=-1){ sb.append(new String(buffer,0,length)); } read.close(); tv_read.setText(sb.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } //存储动态权限的获取,适用于安卓6.0以上的部分机型(兼容模式) private void checkPermission(){ int code=ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); if(code== PackageManager.PERMISSION_GRANTED){//已经有权限 writeBySD(); }else {//没有权限要动态获取,通过弹出的提示框让用户进行选择 ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1); } } //重写onRequestPermissionsResult方法 @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if(requestCode==1&&grantResults[0]==PackageManager.PERMISSION_GRANTED){ writeBySD(); }else { Toast.makeText(this,"访问需要权限,请先获取权限",Toast.LENGTH_SHORT).show(); } } /** * 通过SharedPerference存储 */ private void saveBySP(){ String content=et_save.getText().toString(); SharedPreferences sp=getSharedPreferences(sp_name,MODE_PRIVATE); sp.edit().putString("content",content).commit(); Toast.makeText(this,"存储成功",Toast.LENGTH_SHORT).show(); } /** * 通过SharedPerference读取 */ private void readBySP(){ SharedPreferences sp=getSharedPreferences(sp_name,MODE_APPEND); String content=sp.getString("content","缺省值");//读不到数据就显示默认缺省值 tv_read.setText(content); }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表