简体   繁体   English

如何将文件写入外部存储(SD卡)

[英]How to write files to external storage (sdcard)

I know this may have been answered many times and I have searched, but I can't resolve my problem. 我知道这可能已被多次回答,我已经搜索过,但我无法解决我的问题。 Every time I try to write to sdcard, it appears to be going to internal storage. 每次我尝试写入SD卡时,它似乎都会进入内部存储。 My lines of code are simply 我的代码行很简单

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

When I go to My Files, the directory always appears in /storage/emulated/0 and not on the sdcard. 当我转到“我的文件”时,该目录始终显示在/ storage / emulated / 0中,而不是显示在SD卡上。 I have all the permissions set in the manifest. 我拥有清单中设置的所有权限。 Is there something simple I'm missing? 我有什么简单的东西吗?

PS My device is the Note 3. I am able to copy files to the sdcard manually. PS我的设备是注3.我可以手动将文件复制到SD卡。

How to write files to external storage (sdcard) 如何将文件写入外部存储(SD卡)

External storage is not an "sdcard" on most Android devices. 在大多数Android设备上,外部存储不是“sdcard”。

When I go to My Files, the directory always appears in /storage/emulated/0 and not on the sdcard 当我转到“我的文件”时,该目录始终显示在/ storage / emulated / 0中,而不是显示在SD卡上

That is because /storage/emulated/0 is external storage. 这是因为/storage/emulated/0是外部存储。

Quoting the documentation : 引用文档

Every Android-compatible device supports a shared "external storage" that you can use to save files. 每个Android兼容设备都支持可用于保存文件的共享“外部存储”。 This can be a removable storage media (such as an SD card) or an internal (non-removable) storage. 这可以是可移动存储介质(例如SD卡)或内部(不可移动)存储。

There is almost no support in the Android SDK for alternative storage locations, such as an SD card. Android SDK几乎不支持其他存储位置,例如SD卡。 Android 4.4 has a bit, as is described on the same documentation page: 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. 有时,已经分配内部存储器分区以用作外部存储器的设备也可以提供SD卡插槽。 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. 当这样的设备运行Android 4.3及更低版本时,getExternalFilesDir()方法仅提供对内部分区的访问,并且您的应用程序无法读取或写入SD卡。 Beginning with Android 4.4, however, you can access both locations by calling getExternalFilesDirs(), which returns a File array with entries each location. 但是,从Android 4.4开始,您可以通过调用getExternalFilesDirs()来访问这两个位置,它返回一个包含每个位置条目的File数组。 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(). 如果您想同时访问两个可能的位置,同时也支持Android 4.3及更低版本,请使用支持库的静态方法ContextCompat.getExternalFilesDirs()。 This also returns a File array, but always includes only one entry on Android 4.3 and lower. 这也会返回一个File数组,但在Android 4.3及更低版本中只包含一个条目。

Here I posted the code for finding the sdcard path (in the question's body) 在这里,我发布了用于查找sdcard路径的代码(在问题的正文中)

How can I properly obtain the external SD card path? 如何正确获取外部SD卡路径?

It should solve your problem. 它应该解决你的问题。 The "external" storage is not always your sdcard, because many Android devices consider as external a partition of the internal memory. “外部”存储并不总是您的SD卡,因为许多Android设备将内部存储器视为外部分区。 The path of the real external storage (your sdcard) is setted by the device's constructor, and there's not a standard API for obtaining it. 真实外部存储(您的SD卡)的路径由设备的构造函数设置,并且没有用于获取它的标准API。

Print out 打印

dir.toString()

above and see what you get. 以上,看看你得到了什么。 Then compare it with 然后将其与之比较

File dir = new File(getExternalFilesDir(null) +  File.separator + "mydir");

If you do like the 2nd alternative, you should get (I did anyway) /storage/sdcard0/data/your.package.name/files/mydir and your directory above is mydir, not My Files? 如果你喜欢第二种选择,你应该得到(我做了)/storage/sdcard0/data/your.package.name/files/mydir,你上面的目录是mydir,而不是My Files?

You will need to find the external sd card manually, this depends on the phone. 你需要手动找到外置SD卡,这取决于手机。

See this tutorial where there is an example of how to test the different paths to the sd card. 请参阅本教程 ,其中有一个示例,说明如何测试SD卡的不同路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Android 上使用 Phonegap/Cordova 在外部 sdcard(物理卡)上写入文件? - How to write files on external sdcard (physical card) with Phonegap/Cordova on Android? 如何在我的外部插槽SDCARD中创建新文件(不在内部存储中)-“ / mnt / sdcard2 /” - How to create new files in my external slot SDCARD ( its not in internal storage) - “/mnt/sdcard2/” 如何从外部存储重命名文件(Sdcard) - How to rename the file from external storage (Sdcard) 如何始终将应用程序存储到sdcard /外部存储 - how to always store application to sdcard/external storage 如何从外部sdcard和手机存储中获取所有文件(移动存储内部)? - How to get all files from external sdcard and phone storage (internal with mobile memory)? Android 显示来自 sdcard 和外部存储的媒体文件 - Android Show media files from sdcard as well as external storage 如何仅存储在外部存储存储器/ sdcard0 /上? - How to store only on external storage storage/sdcard0/? Android 4.4:WRITE_EXTERNAL_STORAGE权限对主外部存储/ mnt / sdcard有效吗? - Android 4.4: is WRITE_EXTERNAL_STORAGE permission effective on primary external storage /mnt/sdcard? 将位图写入外部存储(sdcard) - Writing Bitmap to external storage (sdcard) 如何在外部存储中的android中保存,读取和写入文件? - How to save , read and write files in android in external storage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM