在 Android 中解压缩文件的位置

Place to unzip files in Android

由于apk的最大大小为50MB,所以我决定制作apk扩展文件。我知道我可以从此包中获取任何文件(输入流),但我在 LibGDX 中工作,并且没有从输入流加载 textures/music 的选项。 所以,我必须提取文件,加载 texture/music 然后我可以删除文件。 我想我最多需要 50MB space,我有两个选择:

解压缩到:

  1. ContextWrapper.getFilesDir() - 它是 returns 内部文件,但我不知道在那里可以解压多少文件,因为此存储空间与所有应用程序共享。

  2. getExternalStorageDirectory() - 它 returns 外部文件,但根据网站 (developer.android.com) "This directory may not currently be accessible"

哪个目录最适合解压并且始终可用

如果像你说的那样,这里有大量的数据,那么为了提高用户体验(不是所有的手机都有大内存,使用内部存储可能会导致你的应用程序被卸载)你应该使用external storage.

你必须关心几件事:

  • 添加读/写权限
  • 启动应用程序检查文件可用性

添加权限:

<manifest ...>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    ...
</manifest>

检查文件可用性
并启动应用程序,小心并检查外部存储是否可用,否则显示消息以避免异常:

Caution: External storage can become unavailable if the user mounts the external storage on a computer or removes the media, and there's no security enforced upon files you save to the external storage. All applications can read and write files placed on the external storage and the user can remove them.

使用 Context.getCacheDir() 访问您可以提取文件的私人目录。这应该被视为缓存目录,因此您应该在启动时检查该文件,如果丢失则再次解压缩。

此方法不需要任何额外的权限并且更安全。此外,Android API 保证该目录存在。