上一篇:Android数据存储————文件存储(内部存储):https://blog.csdn.net/sinat_29675423/article/details/85699131
存储特征
将数据存储在app目录下,保存到外部存储设备,比如SD卡中,数据既可以公有,也可以私有,空间相对充足。
存储路径
公有:Environment.getExternalStorageDirectory()
私有:/Android/data/<package_name>/files
注意:外部存储权限
访问外部存储权限,API 23以上的应用程序,需要动态申请 外部存储权限
这里推荐使用动态权限处理框架:Easypermission,官方说明:https://github.com/googlesamples/easypermissions
例子:存储、读取字符串
将输入框中的字符串保存到内部存储路径中,并且读取存储的字符串到文本框中显示。
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<EditText
android:id="@+id/etNote"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"/>
<Button
android:id="@+id/btnSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存"
android:onClick="onClick"/>
<Button
android:id="@+id/btnGet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读取"
android:onClick="onClick"/>
<TextView
android:id="@+id/tvContent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
权限配置
应用easypermission框架,在app:gradle中添加:
dependencies {
implementation 'pub.devrel:easypermissions:2.0.0'
}
manifest配置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="你的