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

本文介绍了无法访问辅助存储(外部可移动Micro 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卡),而不能写入真实的外部存储卡"(可移动Micro 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");

I have tried the following things:
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)我尝试了另一张Micro 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卡),而不能写入真实的外部存储卡"(可移动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)

自大约四年前发布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.

或者,使用 Storage Access Framework 并让用户选择放置内容的位置,将生成的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.).

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

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