android - context.openFileOutput() 与 file.createNewFile() 相比?

标签 android

我已使用 File 对象和各种 FileReaders/Writers 和 BufferedReaders/Writers 将我的应用程序的所有数据放在 SD 卡的目录和子目录中。

我突然想到并非所有设备都有 SD 卡,所以我现在有 4 个问题。

  1. 有谁知道目前运行 android 的手机中有多少比例没有安装 SD 卡?

  2. 有没有一种方法可以允许我自己操作手机内存(应用程序目录)的文件结构,就像我可以在 SD 卡上操作一样?

    • 或-
  3. 我是否必须重新实现所有代码才能使用 openFileOutput() 而不是 createNewFile() 在我的应用中复制 SD 卡文件行为?

  4. 为什么 SD 与应用程序目录的文件访问首先存在差异?

最佳答案

1)does anyone know off hand roughly what percentage of handsets running android currently don't implement SD cards?

即使手机可以支持SD卡,也不代表它在给定时刻确实支持,因为PC可以移除或安装SD卡访问或有数百个其他原因。所以,不要把任何重要的东西放在那里。

2)is there a way I can give myself permission to manipulate the file structure of the phones internal memory (application directory) in the same why I can on the SD card?

内存文件不能有任何层级结构,如果你的文件名里面有路径分隔符,openFileOutput() 会给你异常。除此之外,您可以在 /data/data/com.your.app/files 目录中放置多少文件没有限制。而且您不需要任何特定权限即可在那里创建/打开文件。

3)will I have to re-implement all of my code in order to duplicate the SD card file behavior in my app using openFileOutput() instead of createNewFile()?

我不会。基本上,应用程序的所有小文件和/或生命关键文件都进入内存,所有大文件和非必需文件(图像、视频?)都进入 SD 卡。不会有冲突,您的应用程序可以完美启动,如果 SD 卡不可用,请提示。

关于android - context.openFileOutput() 与 file.createNewFile() 相比?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10543963/

相关文章:

Android 下拉菜单(无微调器!)

android - 从 "Choose Application"列表中隐藏 NFC 应用程序/通过外部 NFC Intent 禁用启动

java - 相机拍照旋转

android - 无法为我的 ionic 3 应用程序安装 cordova phonegap-push-plugin

Android透明覆盖工具栏

android - 将文件从模拟器驱动器复制到 SD 卡

android - GestureDetector onScroll() 问题(执行 3 次)

android - 改造 : Custom ConverterFactory only for response

android - Firebase 云消息传递设备 ID : multiple user in same app in same device

android - Android 中的水平滚动文本