美文网首页
安卓--数据存储

安卓--数据存储

作者: 无名感恩 | 来源:发表于2018-04-15 18:25 被阅读0次

    一、知识积累

    1、共享参数SharedPreferences

    适用于简单且孤立的数据、文本形式的数据、持久化保存。
    使用方法:
    1、声明文件名与操作模式
    SharedPreferences mShared = getSharedPreferences("share", MODE_PRIVATE);
    2、存数据

                SharedPreferences.Editor editor = mShared.edit();
                editor.putString("name", name);
                editor.putInt("age", Integer.parseInt(age));
                editor.putLong("height", Long.parseLong(height));
                editor.putFloat("weight", Float.parseFloat(weight));
                editor.putBoolean("married", bMarried);
                editor.putString("update_time", DateUtil.getNowDateTime("yyyy-MM-dd HH:mm:ss"));
                editor.commit(); 
    

    3、遍历获取数据

    Map<String, Object> mapParam = (Map<String, Object>) mShared.getAll();
            for (Map.Entry<String, Object> item_map : mapParam.entrySet()) {
                String key = item_map.getKey();
                Object value = item_map.getValue();
                if (value instanceof String) {
                    desc = String.format("%s\n %s的取值为%s", desc, key, 
                            mShared.getString(key, ""));
                } else if (value instanceof Integer) {
                    desc = String.format("%s\n %s的取值为%d", desc, key, 
                            mShared.getInt(key, 0));
                } else if (value instanceof Float) {
                    desc = String.format("%s\n %s的取值为%f", desc, key, 
                            mShared.getFloat(key, 0.0f));
                } else if (value instanceof Boolean) {
                    desc = String.format("%s\n %s的取值为%b", desc, key, 
                            mShared.getBoolean(key, false));
                } else if (value instanceof Long) {
                    desc = String.format("%s\n %s的取值为%d", desc, key, 
                            mShared.getLong(key, 0l));
                } else {
                    desc = String.format("%s\n参数%s的取值为未知类型", desc, key);
                }
            }
    

    2、数据库SQLite

    常见使用SQLiteOpenHelper类,具体使用步骤:
    1、新建一个数据库操作类 继承SQLiteOpenHelper,重写onCreate 和onUpgrade方法。
    2、封装获取单例对象,打开关闭数据库等方法。
    3、提供CRUD操作。

    3、SD卡文件

    sd卡目录信息、读写文本文件、读写图片文件。
    1、 获取sd卡信息通过Environment类实现,常见的目录:getRootDirectory、getDataDirectory、getDownloadCacheDirectory、getExternalStorageDirectory、getExternalStorageState、Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM))(相机目录)、DIRECTORY_DOCUMENTS、DIRECTORY_DOWNLOADS、DIRECTORY_PICTURES、DIRECTORY_MOVIES、DIRECTORY_MUSIC。
    2、
    (1)FileOutputStream写文件、FileInputStream读文件。
    保存代码:

    public static void saveText(String path, String txt) {
            try {
                FileOutputStream fos = new FileOutputStream(path);
                fos.write(txt.getBytes());
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    

    读取文件代码:

    public static String openText(String path) {
            String readStr = "";
            try {
                FileInputStream fis = new FileInputStream(path);
                byte[] b = new byte[fis.available()];
                fis.read(b);
                readStr = new String(b);
                fis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return readStr;
        }
    

    (2)读写图片使用BufferedOutputStream和BufferedInputStream
    保存图片

    public static void saveImage(String path, Bitmap bitmap) {
            try {
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path));
                bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
                bos.flush();
                bos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    

    获取图片

    public static Bitmap openImage(String path) {
            Bitmap bitmap = null;
            try {
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path));
                bitmap = BitmapFactory.decodeStream(bis);
                bis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return bitmap;
        }
    

    4、全局内存

    5、Application的生命周期

    相关文章

      网友评论

          本文标题:安卓--数据存储

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