[英]Which way is the best to save file on external storage on Android?
Which way is the best to save file on external storage... 哪种方法是将文件保存到外部存储的最佳方式...
a) new File(Enviroment.getExternalStorageDirectory(), "")
a)
new File(Enviroment.getExternalStorageDirectory(), "")
b) new File("/mnt/sdcard/","")
b)
new File("/mnt/sdcard/","")
Enviroment.getExternalStorageDirectory() is the best way to save file into external storage. Enviroment.getExternalStorageDirectory()是将文件保存到外部存储的最佳方法。 Take a look at this http://developer.android.com/reference/android/os/Environment.html
看看这个http://developer.android.com/reference/android/os/Environment.html
Always use 一律使用
Enviroment.getExternalStorageDirectory().getPath()
instead of hard coding as below... 而不是如下所示的硬编码...
/mnt/sdcard/
If you choose hard coding way then the system will throw you below message... 如果您选择硬编码方式,则系统会在消息下方向您显示...
Do not hardcode "/sdcard/"; use Environment.getExternalStorageDirectory().getPath() instead
Another problem is that SDCard
folder name isn't same for all devices... SDCard
name different in Some devices. 另一个问题是
SDCard
文件夹名称在所有设备上都不相同... SDCard
名称在某些设备中不同。
In samsung devices, it is named external_sd
and in this case, the hard coding way will be failed. 在三星设备中,它被命名为
external_sd
,在这种情况下,硬编码方式将失败。 That's why following way is the best... 这就是为什么跟随方式是最好的...
File file = new File(Enviroment.getExternalStorageDirectory().getPath(), "");
一律使用
Enviroment.getExternalStorageDirectory() in place of mnt/sdcard/
Enviroment.getExternalStorageDirectory()
is the best public API for directly accessing the SD card. Enviroment.getExternalStorageDirectory()
是用于直接访问SD卡的最佳公共API。
However, you should consider using the new ACTION_CREATE_DOCUMENT
intent which allows the user to select a location for saving. 但是,您应该考虑使用新的
ACTION_CREATE_DOCUMENT
意图,该意图允许用户选择要保存的位置。
For example, if a device has multiple SD cards, the user can select between them, or they can choose any app participating in the Storage Access Framework. 例如,如果设备具有多个SD卡,则用户可以在它们之间进行选择,也可以选择参与Storage Access Framework的任何应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.