Android App专属文件(app-specific files)存储

这篇具有很好参考价值的文章主要介绍了Android App专属文件(app-specific files)存储。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

前言

有很多场景,我们的App需要创建一些其他App不需要访问、不应该访问的文件,系统提供了一下两种App专属(app-specific)存储目录:

1、内部存储目录(Internal storage directories):

指的是:/data/user/0/packagename/…目录
该目录提供两个目录:一个专门存储持久化文件(getFileDir),一个存储缓存文件(getCacheDir)。
此目录其他App无法访问。

2、外部存储目录(External storage directories):

指的是:/storage/emulated/0/Android/data/packagename/…目录
该目录也提供两个目录:一个专门存储持久化文件(getFileDir),一个存储缓存文件(getCacheDir)。
此目录旨在存储本应用使用的文件,如果需要存储一些可供其他应用访问的共享文件,请使用可共享存储技术(Media content、Documents and other files、Datasets)

App专属存储目录,随着App卸载,会被删除。

一、访问内部存储(Access From Internal Storage)

注意:这个目录往往比较小。内部存储(Internal storage)只提供有限的空间给App专属数据。
Keep in mind, however, that these directories tend to be small. Before writing app-specific files to internal storage, your app should query the free space on the device.
How much space does your data require?
Internal storage has limited space for app-specific data. Use other types of storage if you need to save a substantial amount of data.

1、getFilesDir:访问持久化文件(Access persistent files)

指的是:/data/user/0/packagename/files/…目录

1.1、访问和存储文件(context.getFilesDir())

File file = new File(context.getFilesDir(), filename);

1.2、使用流存储文件(context.openFileOutput())

文件会存储到getFileDir()目录下

String filename = "myfile";
String fileContents = "Hello world!";
try (FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE)) {
    fos.write(fileContents.toByteArray());
}

1.3、使用流访问文件(context.openFileInput())

访问getFileDir()目录下的文件

FileInputStream fis = context.openFileInput(filename);
InputStreamReader inputStreamReader =
        new InputStreamReader(fis, StandardCharsets.UTF_8);
StringBuilder stringBuilder = new StringBuilder();
try (BufferedReader reader = new BufferedReader(inputStreamReader)) {
    String line = reader.readLine();
    while (line != null) {
        stringBuilder.append(line).append('\n');
        line = reader.readLine();
    }
} catch (IOException e) {
    // Error occurred when opening raw file for reading.
} finally {
    String contents = stringBuilder.toString();
}

1.4、查看文件列表(context.fileList())

查看getFileDir()目录下的所有文件

Array<String> files = context.fileList();

2、getCacheDir:创建缓存文件(Create cache files)

指的是:/data/user/0/packagename/cache/…目录

File.createTempFile(filename, null, context.getCacheDir());
File cacheFile = new File(context.getCacheDir(), filename);

二、访问外部存储(Access From External Storage)

Android 4.4及以后,App不再需要请求存储权限就可以访问外部存储中的App专属目录

Android 10(API level 29) 及以后,App默认开启作用域存储模式,App将不能访问非当前应用专属的路径。
只能访问外部存储上当前App专属路径,和App已经创建的指定媒体类型(MediaStore APIs)

On Android 4.4 (API level 19) or higher, your app doesn’t need to request any storage-related permissions to access app-specific directories within external storage. The files stored in these directories are removed when your app is uninstalled.

Scoped storage
To give users more control over their files and to limit file clutter, apps that target Android 10 (API level 29) and higher are given scoped access into external storage, or scoped storage, by default. Such apps have access only to the app-specific directory on external storage, as well as specific types of media that the app has created.

1、getExternalFilesDir:访问持久化文件(Access persistent files)

指的是:/storage/emulated/0/Android/data/packagename/files/…目录

File appSpecificExternalDir = new File(context.getExternalFilesDir(null), filename);

pass null into getExternalFilesDir(). This returns the root app-specific directory within external storage.

2、getExternalCacheDir:创建缓存文件(Create cache files)

指的是:/storage/emulated/0/Android/data/packagename/cache/…目录文章来源地址https://www.toymoban.com/news/detail-425566.html

File externalCacheFile = new File(context.getExternalCacheDir(), filename);

3、Environment.getExternalStorageState:检查外部存储是否可用

// Checks if a volume containing external storage is available
// for read and write.
private boolean isExternalStorageWritable() {
    return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}

// Checks if a volume containing external storage is available to at least read.
private boolean isExternalStorageReadable() {
     return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ||
            Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY);
}

到了这里,关于Android App专属文件(app-specific files)存储的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Android问题笔记二十五:在构建提示“Could not resolve all files for configuration ‘:app:debugRuntimeClasspath”

    专栏分享 点击跳转=Unity3D特效百例 点击跳转=案例项目实战源码 点击跳转=游戏脚本-辅助自动化 点击跳转=Android控件全解手册 点击跳转=Scratch编程案例 点击跳转=软考全系列 众所周知,人生是一个漫长的流程,不断 克服困难 ,不断反思前进的过程。在这个过程中会产生很多对

    2024年02月03日
    浏览(38)
  • vue+cordova混合开发APP中向手机本地存储/读取文件

    采用 vue + cordova 开发的App, 项目首屏为 three.js 编写的3D场景 3D模型的数据是首屏启动后前端调用接口获取的json数据,数据大小30M. 用户在首页与其他页面切换时,调用该接口时间过长,并消耗大量流量 解决思路: 首页页面不销毁,做缓存. (可行,但three.js的页面内存占用量极大,不做特

    2024年02月09日
    浏览(32)
  • uni-app实现上传文件至云存储的三种方式

    目录 前言 1.在uniCloud WEB控制台中可以直接上传文件 2.客户端api上传或者组件 组件上传 客户端手动api上传 3. 云函数上传文件到云存储 总结 开发者使用 uniCloud 的云存储,无需再像传统模式那样单独去购买存储空间、CDN映射、流量采购等,本篇文章主要讲解如何使用uni-app实现

    2024年02月08日
    浏览(44)
  • 安卓APP证书Android签名证书.keystore文件制作生成

    将app发布到应用宝时,而上传安装包时提示如下图时, 但您或您的团队从未上传该应用,您可以通过认领功能认领应用。认领成功后该应用会自动转入您的账户下,同时保留应用全部数据,您可以在应用管理中进行管理。 1.点击认领应用 ,确认需认领应用的包名是否是你的

    2024年02月02日
    浏览(40)
  • 基于Android平台的记事本软件(Android Studio项目+报告+app文件)

    移动应用开发技术 期末考核报告 题    目:         基于 Android 平台的记事本软件              学生姓名                               学生学号                               专      业                            班     级

    2024年02月08日
    浏览(40)
  • Android 浅度解析:mk预置AAR、SO文件、APP包和签名

    在Android开发中,经常需要使用到第三方库,这些库通常以AAR或JAR的形式提供。在本文中,将详细介绍如何在Android.mk文件中引用AAR文件。 AAR(Android Archive)文件是Android库项目的打包格式,它包含了Java类、资源文件、清单文件和可选的本地库文件。AAR文件是一种方便的方式来

    2024年02月13日
    浏览(64)
  • Android Studio项目打包生成可安装在自己手机上的App安装包文件

    点击上方 “ 码农的后花园 ”, 选择 “ 星标 ”  公众号 精选文章,第一时间送达 Android 打包 其实我们现在Android手机上所有的应用都是.apk文件,只不过分为系统自带和第三方,一个.apk文件本质其实就对应于你手机上的一个应用App程序,比如支付宝,淘宝。 .apk文件就是一个

    2024年02月05日
    浏览(78)
  • uniapp 发送全文件 支持App端ios、android,微信小程序,H5

    由于uniapp提供的API在app端只能上传图片和视频,不能上传其他文件,说以只能借助插件了。  ios端用的这个插件 获取到文件对象 免费的 ios-uniapp 文件选取word,pdf,xls等文件 - DCloud 插件市场 uniapp iOS文件选取 iOS选取text,pdf,word,doc,xls,ppt https://ext.dcloud.net.cn/plugin?id=1311 这个是返回一

    2024年02月16日
    浏览(33)
  • uni-app uni-file-picker文件上传实现拍摄从相册选择获取图片上传文档服务器(H5上传-微信小程序上传)

    前言 最近在使用uni-app写H5移动端,有一个从手机拍摄从相册选择获取图片上传到文档服务器功能。 查阅uni-app发现关于上传图片,uni-file-picker文件上传,uni.chooseImage,uni.uploadFile H5上传时它和pc端原理差不多,都是file对象上传,PC端是通过new file对象,uni-app是直接提供了 微信

    2024年02月15日
    浏览(45)
  • 解决npm报错Error:EEXIST: file already exists, mkdir “文件路径“,yarn create vite-app 报文件名、目录名或卷标语法不正确

    我这里出现错误是因为在配置npm命令目录与npm全局安装位置时目录创建失败,但是在执行 命令之后在本地的\\\" .yarnrc \\\"文件中 \\\" global-folder \\\" 属性被写入,之后会卡在 这个问题 在c盘的对应用户的文件夹下找到 .yarnrc 文件,打开它,将其配置为正确的路径即可 我这里是因为yarn的

    2024年02月08日
    浏览(46)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包