无法在SD卡上写入文件 [英] Can't write file on SD-card
问题描述
我正在尝试在SD卡上写照片而没有成功.
I'm trying to write a photo on the SD card without success.
我具有写在可移动存储中的权限,并且已安装sd卡.
I've got the permissions to write in the removable storage and the sd card is mounted.
此外,我检查了SD卡的路径是否存在,并获得了肯定的结果.
Also, I checked that the path to the SD card exists and I have obtained a positive result.
失败的地方是当我使用mkdir()
函数时.返回false且未创建文件.
Where it fails is when I use the mkdir()
function. It returns false and no file is created.
我已经测试了三星A6(棉花糖)和三星Tab4(棒棒糖)
I have tested on both a Samsung A6(Marshmallow) and a Samsung Tab4(Lollipop)
这是我用来检索SD卡路径的代码段.
因为标准过程不适用于Samsung设备,所以我使用了从stackoverflow答案
This is the snippet of code I'm using to retrieve the path to the SD-card.
Because the standard procedure didn't work with Samsung devices, I'm using this snippet of code that I took from a stackoverflow answer
此功能从Samsung A6返回的路径是/storage/6DD9-1D15.
The path returned with this function from the Samsung A6 is /storage/6DD9-1D15.
public String[] getStorageDirectories() {
String[] storageDirectories;
String rawSecondaryStoragesStr = System.getenv("SECONDARY_STORAGE");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
List<String> results = new ArrayList<String>();
File[] externalDirs = getApplicationContext().getExternalFilesDirs(null);
for (File file : externalDirs) {
String path = file.getPath().split("/Android")[0];
if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Environment.isExternalStorageRemovable(file))
|| rawSecondaryStoragesStr != null && rawSecondaryStoragesStr.contains(path)) {
results.add(path);
}
}
storageDirectories = results.toArray(new String[0]);
} else {
final Set<String> rv = new HashSet<String>();
if (!TextUtils.isEmpty(rawSecondaryStoragesStr)) {
final String[] rawSecondaryStorages = rawSecondaryStoragesStr.split(File.pathSeparator);
Collections.addAll(rv, rawSecondaryStorages);
}
storageDirectories = rv.toArray(new String[rv.size()]);
}
return storageDirectories;
}
这是我用来在SD卡下的DCMI目录中写入文件夹的代码(我将在其中放置照片)
This, instead, is the code I'm using to write a folder in the DCMI Directory under the SD-card(in which I will put the photos)
public void mkFolder(String folderPath) { // make a folder under Environment.DIRECTORY_DCIM
File folder = new File(folderPath);
try {
// MKDIRS returns false
if (folder.mkdirs()) {
Log.d(TAG, "folder created:" + folder.toString());
} else {
Log.d(TAG, "create folder fails:" + folder.toString());
}
} catch (Exception ecp) {
ecp.printStackTrace();
}
}
推荐答案
在API级别18以下,您的getStorageDirectories()
将通过其else
块.该块假定:
On API Level 18 and below, your getStorageDirectories()
will go through its else
block. That block assumes:
-
SECONDARY_STORAGE
环境变量存在...这不是必需的 -
SECONDARY_STORAGE
包含分隔的目录列表...不是必需的 - 目录列表与可移动存储选项相匹配...不是必需的
- 您可以使用这些目录...不是必需的
- that the
SECONDARY_STORAGE
environment variable exists... which is not required - that the
SECONDARY_STORAGE
contains a delimited list of directories... which is not required - that the list of directories matches removable storage options... which is not required
- that you can work with those directories... which is not required
在API级别19+上,您的getStorageDirectories()
代码将通过其if
块.在那里,您可以开始并调用getExternalFilesDirs()
.如果该方法返回2个以上的项目,则第二个及随后的项目将指向可移动存储,尤其是指向可读取和写入的可移动存储中的位置.然后,您的代码假定:
On API Level 19+, your getStorageDirectories()
code will go through its if
block. There, you start off fine, calling getExternalFilesDirs()
. If that method returns 2+ items, the second and subsequent ones point to removable storage, and specifically point to places on removable storage where you can read and write. Then, your code assumes:
- 该目录具有
/Android
路径段...这不是必需的 -
/Android
之前的目录路径部分代表您可以在其中创建文件和目录的位置... 从来都不是
- that the directory has an
/Android
path segment... which is not required - that the portion of the directory path preceding
/Android
represents a location in which you can create files and directories... which is never true
您无权访问可移动存储的文件系统级权限,除了由getExternalFilesDirs()
之类的方法返回的特定目录中的除外.
You do not have filesystem-level access to removable storage, except in the specific directories returned by methods like getExternalFilesDirs()
.
所以,要么:
-
固定在
getExternalFilesDirs()
返回的特定位置,或
切换为使用存储访问框架,允许用户选择将内容存储在何处(可能是可移动存储,也可能不是可移动存储)
Switch to using the Storage Access Framework, allowing the user to choose where to store content (which may or may not be removable storage)
这篇关于无法在SD卡上写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!