gpt4 book ai didi

android - 如何将文件写入外部存储(sdcard)

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:48:56 27 4
gpt4 key购买 nike

我知道这个问题可能已经回答了很多次,我也进行了搜索,但我无法解决我的问题。每次我尝试写入 SD 卡时,它似乎都会写入内部存储。我的代码行很简单

File root = android.os.Environment.getExternalStorageDirectory(); 
File dir = new File (root.getAbsolutePath() +"/mydir");
dir.mkdirs();

当我转到“我的文件”时,目录总是出现在/storage/emulated/0 而不是在 sdcard 上。我在 list 中设置了所有权限。我缺少什么简单的东西吗?

附言我的设备是Note 3,我可以手动复制文件到sdcard。

最佳答案

How to write files to external storage (sdcard)

在大多数 Android 设备上,外部存储不是“SD 卡”。

When I go to My Files, the directory always appears in /storage/emulated/0 and not on the sdcard

那是因为/storage/emulated/0是外部存储。

引用 the documentation :

Every Android-compatible device supports a shared "external storage" that you can use to save files. This can be a removable storage media (such as an SD card) or an internal (non-removable) storage.

Android SDK 几乎不支持替代存储位置,例如 SD 卡。 Android 4.4 有一点,如同一文档页面所述:

Sometimes, a device that has allocated a partition of the internal memory for use as the external storage may also offer an SD card slot. When such a device is running Android 4.3 and lower, the getExternalFilesDir() method provides access to only the internal partition and your app cannot read or write to the SD card. Beginning with Android 4.4, however, you can access both locations by calling getExternalFilesDirs(), which returns a File array with entries each location. The first entry in the array is considered the primary external storage and you should use that location unless it's full or unavailable. If you'd like to access both possible locations while also supporting Android 4.3 and lower, use the support library's static method, ContextCompat.getExternalFilesDirs(). This also returns a File array, but always includes only one entry on Android 4.3 and lower.

关于android - 如何将文件写入外部存储(sdcard),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21095360/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com