美文网首页
安卓常见数据存储

安卓常见数据存储

作者: 俗人彭jin | 来源:发表于2021-09-14 10:38 被阅读0次
image.png

SharedPreferences模式

image.png
![image.png](https://img.haomeiwen.com/i12474760/8ab80938a1bdc110.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

// 存储的操作 
 String strName =  editTextName.getText().toString();
                String strPwd =  editTextPwd.getText().toString();
                // SharedPreferences 读取
                Log.e("strName",strName);
                Log.e("passWord",strPwd);
                if(strName.equals("admin") && strPwd.equals("123")) {
                    // 2.1  存储信息到SharedPreferences 参数1:参数名称,参数2: 参数模式
                    SharedPreferences SharedPreferences = getSharedPreferences("myName",MODE_PRIVATE);
                    // 2.2 获取editor对象
                    android.content.SharedPreferences.Editor editor = SharedPreferences.edit();
                    // 2.3.存储信息
                    editor.putString("userName",strName);
                    editor.putString("passWord",strPwd);
                    // 2.4 指定提交操作
                    editor.commit();

找到存储的数据在哪里


image.png
    // 获取数据操作
    // 获取参数
        SharedPreferences shared = getSharedPreferences("myName",MODE_PRIVATE);
        //获取参数的默认值
        String user =  shared.getString("userName","");
        String pwd = shared.getString("passWord","");
        editTextName.setText(user);
        editTextPwd.setText(pwd);

外部存储

image.png
image.png
// 点击按钮去写入文件和读取文件操作
    public void FnOperate(View view) {
        if(!isGrantExternalRW(this)){ // 动态授权
            return;
        }
        // Environment.getExternalStorageDirectory().getAbsolutePath() 绝对路径
        String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/pjtest.txt";
//        Environment.getExternalStorageState().equals("mounted") // 是否有内存卡
        Log.e("TAG",path+Environment.getExternalStorageState().equals("mounted"));
        switch (view.getId()){
            case R.id.save:
                File f = new File(path);
                try {
                    if (!f.exists()) {
                        f.createNewFile();
                    }

                    FileOutputStream fos = new FileOutputStream(path,true);
                    String str = infoEdt.getText().toString();
                    fos.write(str.getBytes());
                }catch (IOException ioe){
                    ioe.printStackTrace();
                }
            case R.id.read:
                // tex
                try {
                    FileInputStream fis = new FileInputStream(path);
                    byte[] b = new byte[1024];
                    int len = fis.read(b);
                    String str2 = new String(b,0,len);
                    Log.e("str2",str2);
                    tex.setText(str2);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

动态授权操作

// 动态申请权限
   public static boolean isGrantExternalRW(Activity activity) {
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && activity.checkSelfPermission(
               Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
           activity.requestPermissions(new String[]{
                   Manifest.permission.READ_EXTERNAL_STORAGE,
                   Manifest.permission.WRITE_EXTERNAL_STORAGE
           }, 1);
           return false;
       }
       return true;
   }
image.png

新版本存储地址

image.png

内部存储

image.png
image.png

相关文章

  • 性能优化之存储优化

    一、安卓4种存储方式 安卓系统4种基本的存储方式 使用SharedPreferences存储数据 文件存储数据 S...

  • 安卓常见数据存储

    SharedPreferences模式 找到存储的数据在哪里 外部存储 动态授权操作 新版本存储地址 内部存储

  • 安卓--数据存储

    一、知识积累 1、共享参数SharedPreferences 适用于简单且孤立的数据、文本形式的数据、持久化保存。...

  • 安卓——之ListView和SQLite数据库写一个联系人

    安卓的数据存储分为五大类:SharedPreferences存储、文件存储(内部存储、外部SD卡存储)、SQLit...

  • 安卓数据存储方案

    持久化技术简介 数据持久化就是指将那些内存中的瞬时数据保存在存储设备中,保证即使在手机或电脑关机的情况下,这些数据...

  • Android中存取简单的数据

    Android的存储 在安卓中,我们可以将数据存储在应用的专属路径下或者存储在外部存储空间中,这是数据的存储位置。...

  • 消息机制--handler

    ThreadLocal: 用来存储不同线程中的数据,在安卓消息机制中,threadloacl用来存储每个线程的lo...

  • 安卓的存储机制

    安卓的文件存储分为内部存储和外部存储,在安卓早期,手机内置存储很小,都是用于应用的私有存储空间,外部存储就是sdc...

  • sqlite和GreenDao(一)

    安卓编程,存储数据,特别是结果比较规则的数据,使用数据库来存储是比较合适的,常用的数据库有原生的sqlite和第三...

  • 【资源汇总】Android应用解决方案全攻略

    安卓广告联盟解决方案: 安卓消息推送解决方案: 安卓应用安全解决方案: 安卓统计分析解决方案: 安卓后端存储解决方...

网友评论

      本文标题:安卓常见数据存储

      本文链接:https://www.haomeiwen.com/subject/hkaiwltx.html