无法访问辅助存储(外部可移动微型 SD 卡) [英] Can't access the secondary storage (the external removable micro sd card)

本文介绍了无法访问辅助存储(外部可移动微型 SD 卡)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读真正的问题之前,请先阅读我的问题介绍:

Please read my question introduction before reading the real question:

在 Android 中有一个 internal storage 和一个 external storage.不要混淆,因为 internal storage 是指只能被应用访问的文件,而 external storage 是指所有应用都可以访问的普通文件并且可以在真实的内部存储卡(手机的sd卡​​)和真实的外部存储卡(可移动的micro sd卡)中找到.

In Android there are an internal storage and an external storage. Don't be confused, because the internal storage relates to files that are accessible only by the app, and the external storage relates to normal files that can be accessed by all apps and can be found both in the real internal storage card (the phone's sd card) and in the real external storage card (the removable micro sd card).

我手机sd卡的路径(你不能取出的那个)是:/storage/emulated/0.
我的micro sd卡(可以插入或移除的)的路径是:/storage/external_SD.

My path of the phone's sd card (the one that you can't remove) is: /storage/emulated/0.
My path of the micro sd card (the one that you can insert or remove) is: /storage/external_SD.

请注意:当我使用 Environment.getExternalStorageDirectory().getPath() 时,我得到:/storage/emulated/0.
这是真正的内部存储卡(手机的sd卡​​)而不是真正的外部存储卡(可移动micro sd卡)的路径.

Please note: When I use Environment.getExternalStorageDirectory().getPath() i get: /storage/emulated/0.
This is the path of the real internal storage card (the phone's sd card) and not the real external storage card (the removable micro sd card).

我的问题是我可以写入真正的内部存储卡"(手机的 SD 卡),但不能写入真正的外部存储卡"(可移动的微型 SD 卡).我需要将文件保存在 micro SD 卡上.

My problem is that I can write to the "real internal storage card" (the phone's sd card) but not to the "real external storage card" (the removable micro sd card). And I need to save files on the micro sd card.

我尝试了以下几点:
FileOutputStream fos;
1) fos = new FileOutputStream(System.getenv("SECONDARY_STORAGE") + "/test.txt");
2) fos = new FileOutputStream("/storage/external_SD/test.txt");

在两种方式中,我都将 micro sd 卡的路径放在 FileOutputStream 的参数中,这样它就会将文件test.txt"保存在 micro sd 卡上,但我的应用程序崩溃了什么也没发生!
另外,我也尝试创建一个文件夹,但没有成功.

In both ways, I put the path of the micro sd card at the parameter of the FileOutputStream so it will save the file 'test.txt' on the micro sd card, but my app crashes and nothing happens!
In addition, I tried also to create a folder, but it didn't work.

然而,当我把手机的sd卡​​的路径,它工作!
也可以创建文件夹.

However, when I put the path of the phone's sd card, it works!
And also folder creation works.

但我需要在 micro sd 卡上写入文件.

But I need to write files on the micro sd card.

在给我答复之前请注意以下事项:
1)我在清单中添加了以下权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
2) 在试用该应用程序之前,我曾尝试从计算机上断开电缆连接.
3) 我尝试禁用调试模式.
4)我尝试了另一张微型 SD 卡.
5) 我的智能手机是 LG Optimus L7 (LG P714).
6) 我有 Android KitKat (4.4.2).
7) 当我在另一部智能手机(三星 Galaxy S3)上试用该应用程序时,当我将文件保存在 micro sd 卡上时它仍然崩溃,但是当我保存一个文件夹时它成功了!

Please note the following things before giving me an answer:
1) I put the following permissions in the manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
2) I had tried disconnecting the cable from my computer before I tried the app.
3) I tried disabling debugging mode.
4) I tried another micro sd card.
5) My smartphone is LG Optimus L7 (LG P714).
6) I have Android KitKat (4.4.2).
7) When I tried the app on another smartphone (Samsung Galaxy S3) it still crashed when I saved files on the micro sd card, but when I saved a folder it succeeded!

有人知道是什么问题吗?

Does anyone know what is the problem?

推荐答案

我的问题是我可以写入真正的内部存储卡"(手机的 SD 卡),但不能写入真正的外部存储卡"(可移动的微型 SD 卡)

My problem is that I can write to the "real internal storage card" (the phone's sd card) but not to the "real external storage card" (the removable micro sd card)

自大约四年前发布的 Android 4.4 以来就是这种情况.

That's been the case since Android 4.4, released about four years ago.

但我需要在 micro sd 卡上写入文件.

But I need to write files on the micro sd card.

您可以在 Context 上使用 getExternalFilesDirs()getExternalCacheDirs()getExternalMediaDirs().请注意这些方法名称的复数形式.如果这些返回 2 个以上的项目,则第二个和后续的项目是可移动存储上的位置,对于您的应用而言是唯一的,您可以从中读取和写入.

You can use getExternalFilesDirs(), getExternalCacheDirs(), and getExternalMediaDirs() on Context. Note the plural form of these method names. If these return 2+ items, the second and subsequent ones are locations on removable storage, unique to your app, that you can read from and write to.

或者,使用存储访问框架并让用户选择放置内容的位置,您可以使用生成的 UriContentResolveropenOuptutStream() 写入该位置.这将允许用户使用外部存储、可移动存储以及第三方应用(Google 云端硬盘、SMB/CIFS 文件服务器等)支持的位置.

Alternatively, use the Storage Access Framework and let the user choose where to put the content, where you use the resulting Uri with ContentResolver and openOuptutStream() to write to that location. This would allow the user to work with external storage, removable storage, plus locations supported by third-party apps (Google Drive, SMB/CIFS file servers, etc.).

这篇关于无法访问辅助存储(外部可移动微型 SD 卡)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆