【发布时间】:2015-10-21 06:26:42
【问题描述】:
正如标题所指出的,我无法将文件写入外部存储。我的调试设备是 Nexus 5。问题是,我能够从设备中完美地读取文件(我一直在尝试使用下载文件夹中的文件)但无法写入它们。我知道我必须在设备未连接到计算机时执行此操作。但它也不起作用。
事实上,我已经尝试在写入 SD 卡之前读取它的状态(当然没有用)。当设备连接到我的电脑时,状态显示为“已安装”。我将状态与Environment.MEDIA_MOUNTED_READ_ONLY 和Environment.MEDIA_MOUNTED 进行了比较,但没有成功。我的设备没有处于这些状态。
您必须知道的一件事是我的手机没有外部 SD 卡,因为它是内部的。这导致我的设备有一个用于外部存储的“/storage/emulated/0/...”目录。
我还必须指出,我在我的 Android Manifest 中实现了以下标签:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE"/>
我不知道会发生什么。另一件可能有帮助的事情是,我尝试使用 winrar(适用于 Android)管理文件,并且我已经能够通过连接到我的 PC 的设备以及不连接它来删除文件。所以我不知道该怎么办。
我用来编写文件的代码如下。请记住,它应该读取图像文件(确实如此),将其转换为字符串,将其转换回图像,然后将其保存到下载文件夹:
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/base_image.jpg");
// Reading a Image file from file system
FileInputStream imageInFile = new FileInputStream(file);
byte imageData[] = new byte[(int) file.length()];
imageInFile.read(imageData);
// Converting Image byte array into Base64 String
String imageDataString = encodeImage(imageData);
// Converting a Base64 String into Image byte array
byte[] imageByteArray = decodeImage(imageDataString);
File newFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "converted_image.jpg");
//Write a image byte array into file system
FileOutputStream imageOutFile = new FileOutputStream(newFile);
imageOutFile.write(imageByteArray);
imageInFile.close();
imageOutFile.close();
我该怎么办?
【问题讨论】:
-
你确定文件退出写入它吗?
-
我正在创建一个新文件。或者,至少,这就是我正在尝试的。我在尝试创建文件时收到文件未找到异常和 EACCES(权限被拒绝)错误。这是我的主要问题。我做错了吗?
标签: java android android-external-storage nexus-5