Android数据存储————文件存储(外部存储)

本文介绍了Android数据在外部存储的文件存储方式,包括存储特征、存储路径,特别强调了在API 23以上版本需要动态申请外部存储权限,并提供了一个使用Easypermission框架进行权限处理及存储读取字符串的实例。
摘要由CSDN通过智能技术生成

上一篇: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="你的
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值