android 写入外部存储,在Android中将文件写入外部存储器失败

正如标题所指出的那样,我在将文件写入外部存储器时遇到了麻烦.我的调试设备是Nexus 5.问题是,我能够从设备中完美地读取文件(我一直在尝试使用下载文件夹中的文件),但无法编写它们.我知道我必须在设备未连接到计算机时执行此操作.但它也不起作用.

事实上,我已经尝试在写入之前阅读SD卡的状态(当然,这不起作用).当设备连接到我的PC时,状态显示为"已安装".我比较了状态Environment.MEDIA_MOUNTED_READ_ONLY,Environment.MEDIA_MOUNTED没有任何成功.我的设备不属于这些状态.

你必须知道的一件事是我的手机没有外部SD卡,因为它是一个内部SD卡.这导致我的设备具有用于外部存储的"/ storage/emulated/0/..."目录.

我还必须指出,我在Android Manifest中实现了以下标记:

我对可能发生的事情没有任何线索.另一件可能有用的事情是我尝试使用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();

我该怎么办?