android - KITKAT中保存数据到/external sdcard/Android/data/<app package name>

标签 android android-4.4-kitkat

我阅读了很多关于 KITKAT 和防止写入辅助外部 SD 卡的内容。 这似乎是谷歌的一项新政策。我还阅读了有关将应用程序数据存储在/externalsdcard/Android/data/下的内容。

现在是我的问题,因为我无法重现或测试: 如果不允许app直接在下面写数据 , 是否可以将应用的数据存储在/Android/data/下?

如果可能的话。有人可以分享代码示例吗?

最佳答案

  • 永远不要假设可移动辅助存储大于主内部存储。在大多数现代设备上,内部存储空间可以高达 64 GB,但 SD 卡可以小到 1 MB(如果用户插入小的旧卡)。

  • 使用当前的 API 无法区分哪个是“外部”。

所以正确的方法是使用 Context.getExternalFilesDirs()它返回所有外部存储,您可以在没有任何权限的情况下存储应用程序特定的文件。

使用 StatFs 找到可用的最大存储,并将数据写入其中。

final File[] dirs = context.getExternalFilesDirs(null); //null means default type
//find a dir that has most of the space and save using StatFs

正如文档所说,

Returns absolute paths to application-specific directories on all external storage devices where the application can place persistent files it owns. These files are internal to the application, and not typically visible to the user as media.

This is like getFilesDir() in that these files will be deleted when the application is uninstalled, however there are some important differences:

External files are not always available: they will disappear if the user mounts the external storage on a computer or removes it. There is no security enforced with these files.

External storage devices returned here are considered a permanent part of the device, including both emulated external storage and physical media slots, such as SD cards in a battery compartment. The returned paths do not include transient devices, such as USB flash drives.

An application may store data on any or all of the returned devices. For example, an app may choose to store large files on the device with the most available space, as measured by StatFs.

No permissions are required to read or write to the returned paths; they are always accessible to the calling app. Write access outside of these paths on secondary external storage devices is not available.

The first path returned is the same as getExternalFilesDir(String). Returned paths may be null if a storage device is unavailable.

关于android - KITKAT中保存数据到/external sdcard/Android/data/<app package name>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22414417/

相关文章:

java - android.intent.action.DOWNLOAD_COMPLETE 不会在下载失败时收到

android - 如何更改 Play 商店控制台帐户电子邮件 ID

android - 旋转动画不适用于 Android 4.4.4

java - 安卓 5.1 : can not capture and get full-size image

Android Intel x86 Emulator KitKat (19) 无法启动

Android Camera - 录制视频时预览放大

Android 工具栏不从 Backstack 上的 fragment 调用 onOptionsItemSelected

android - Android KitKat 上的对话框似乎被剪掉了

java - 如何使用 jni 指定可变数量的参数