本文整理自网络,侵删。
目录
- 1、存储在App内部
- 2、SD卡外部存储
- 3、SharedPreferences存储
- 4、使用SQLite数据库存储
- 4.1 自己完成一个BaseDao类
- 4.2 使用Google写的API处理
- 4.3 事务使用
- 总结
1、存储在App内部
最简单的一种。在尝试过程中发现,手机中很多文件夹都没有权限读写。我们可以将我们需要写的文件存放到App中的files
文件夹中,当然我们有权限在整个App中读写文件
可以通过API获取一个file对象,这里的this就是MainActivity类
// 获取当前包下的files路径 /data/data/top.woodwhale.qqlogin/files File filesDir = this.getFilesDir();
之后就可以通过文件输出流写入文件:
File filesFile = new File(filesDir,"info.txt"); boolean flag = (filesFile.exists() || filesFile.createNewFile()); FileOutputStream fos = new FileOutputStream(file); fos.write((ac+"***"+pwd).getBytes(StandardCharsets.UTF_8)); fos.close();
写入成功:
当然,我们既然在这个App中都有权限,那么所有目录都可以写:
// 写入到自己有权限写的地方 File file = new File("/data/data/top.woodwhale.qqlogin/info.txt");
2、SD卡外部存储
虽然现在很多的手机都不支持SD卡了,但是仍然有平板使用。
直接放出一个Activity类,其中调用了nvironment.getExternalStorageDirectory();
方法类获取一个sd卡file对象,使用Formatter.formatFileSize(this,externalStorageDirectory.getFreeSpace()));
Formatter类中的转化,将long类型转化为大小类型,同时调用sd卡file对象的getFreeSpace()
方法,获取卡中剩余的空间,之后就是写入externalStorageDirectory.getPath()
卡中的路径
public class SdcardActivity extends Activity { private Button btn; public static String TAG = "SdcardActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sc_card_rw); btn = this.findViewById(R.id.bt_sdw); btn.setOnClickListener(view -> { File externalStorageDirectory = Environment.getExternalStorageDirectory(); Log.d(TAG, "sd卡路径是:"+externalStorageDirectory.getPath()); Log.d(TAG,"sd卡剩余空间是"+ Formatter.formatFileSize(this,externalStorageDirectory.getFreeSpace())); File file = new File(externalStorageDirectory,"love.txt"); try { boolean flag = file.exists() || file.createNewFile(); if (flag) { FileOutputStream fos = new FileOutputStream(file); fos.write("woodwhale love sheepbotany".getBytes(StandardCharsets.UTF_8)); fos.close(); } } catch (Exception e) { e.printStackTrace(); } }); } }
但是,在此之前,我们需要一个SD卡的读写权限,我们在AndrodiManifest.xml
中配置下面的ses-permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
最终,在我们的sd卡中看到如下结果,证明写入成功:
3、SharedPreferences存储
SharedPreferences
是android下的一个类,功能就是记录偏好设置,形成一个xml文件
我们可以用SharedPreferences
来存储一些信息。
例如常见的这种:
我们勾选之后,再次打开app仍然处于勾选状态。
那么这种情况如何实现呢?
通过xml生成上面的布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="80dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_centerVertical="true"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="未知来源" android:textColor="@color/teal_200" android:layout_marginLeft="10dp" android:textSize="20sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="运行安装未知来源的应用" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:textSize="18sp"/> </LinearLayout> <Switch android:id="@+id/sw_source" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginTop="30dp" android:layout_marginRight="10dp" /> </RelativeLayout>
我们把Switch这个选择框在activity类中赋予一个变量,给他加上一个OnCheckedChangeListener
,再使用SharedPreferences
来进行设置偏好,整体代码如下
package top.woodwhale.qqlogin; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; import android.widget.CompoundButton; import android.widget.Switch; import androidx.annotation.Nullable; public class PreferenceDemoActivity extends Activity { private Switch sw; public static String TAG = "PreferenceDemoActivity"; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pre_demo); sw = (Switch) this.findViewById(R.id.sw_source); SharedPreferences settingInfo = this.getSharedPreferences("settingInfo", MODE_PRIVATE); SharedPreferences.Editor edit = settingInfo.edit(); sw.setOnCheckedChangeListener(new MyListener(edit)); boolean state = settingInfo.getBoolean("state", true); Log.d(TAG,"STATE=="+ state); sw.setChecked(state); } } // 改变状态的监听器 class MyListener implements CompoundButton.OnCheckedChangeListener { SharedPreferences.Editor editor; public MyListener(SharedPreferences.Editor editor) { this.editor = editor; } @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { Log.d(PreferenceDemoActivity.TAG,"current state : "+ b); editor.putBoolean("state",b); // 要保存的数据类型 editor.commit(); // 保存数据 } }
其中,editor的功能是保存数据
其次,为了每次打开App都可以看到我们的配置,通过读取偏好配置文件,设置switch框的勾选
这样就可以同步偏好设置的勾选啦!
最后我们可以在手机内部看到我们写入的偏好设置xml文件了,这样也属于存储在App内部
4、使用SQLite数据库存储
Android设备自带SQLite数据库,如果掌握过mysql,那么SQLite非常容易上手,且不说提供了非常简便的API,就算是自己写也比JDBC简单!
首先我们不适用提供的API来实现一次增删改查!
4.1 自己完成一个BaseDao类
BaseDao类本来是用来连接数据库等基础的,具体的增删改查应该在service层实现,但为了这里测试,我们将crud的方法写入到BaseDao类中封装起来。具体架构如下:
相关阅读 >>
python Sqlite3以字典形式返回查询结果的实现方法
ubuntu 解决libSqlite3-0依赖的问题(推荐)
Sqlitemanager怎么激活Sqlite数据库管理软件激活图文教程
Sqlite之c#版 system.data.Sqlite使用方法
更多相关阅读请进入《Sqlite》频道 >>
数据库系统概念 第6版
机械工业出版社
本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。
转载请注明出处:木庄网络博客 » Android中的存储详解
相关推荐
评论
管理员已关闭评论功能...