简体   繁体   English

哪种方法最适合在Android设备上将文件保存到外部存储中?

[英]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.

相关问题 Android:在外部存储设备上保存图像的最佳方法 - Android: Best way to save image at external Storage 如何在Android外部存储中保存公共文件 - how to Save Public File in Android external storage 如何在 Android 中正确地将文件保存到外部存储? - How to save file to external storage properly in Android? 无法将文件保存到 android 中的外部存储目录 - Unable to save file to an external storage directory in android 哪种方法是通过Google API为Android应用程序使用文件存储的最佳方式? Google云端硬盘SDK或Google云存储API? - Which Is the best way to use file storage for Android app by Google API? Google Drive sdk or Google cloud Storage API? Android将图像保存到外部存储 - Android save image to external storage Android将图像保存在外部存储中 - Android save image in external storage 将传入的短信保存到Android外部存储中的文件中 - Save incoming text message in file in external Storage in Android 在Android中安装时如何在外部存储上保存大文件? - How to save a large file on external storage at Installation in Android? 如何将文件保存到 Android Q (API 29) 上的公共(外部)存储? - How to save file to public (external) storage on Android Q (API 29)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM