我正在尝试将图像从应用程序本地数据文件夹保存到外部存储。我的清单包含以下内容(在清单的应用程序标签之前):
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18"/>
当我尝试以下操作时
try {
InputStream in = new FileInputStream(filePath);
File outPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File outFile = new File(outPath, "mypicture.jpg");
//try fails at this line
OutputStream out = new FileOutputStream(outFile);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (IOException e) {
e.printStackTrace();
}
我收到此错误:
java.io.FileNotFoundException: /storage/emulated/0/Pictures/mypicture.jpg: open failed: EACCES (Permission denied)
我还尝试了稍微不同的输出路径:
String sdCardPath = Environment.getExternalStorageDirectory() + "/MyFolder";
new File(sdCardPath).mkdirs();
File outFile = new File(sdCardPath, "mypicture.jpg");
但这也给了我一个错误:
java.io.FileNotFoundException: /storage/emulated/0/MyFolder/mypicture.jpg: open failed: ENOENT (No such file or directory)
该设备运行的是 Android 4.4.2,因此不需要在运行时请求权限(据我所知)can't要求他们)。
为了允许将文件保存到外部存储,是否还可能缺少其他内容?