尝试将图像保存到外部存储并获取上述异常。已阅读并尝试了不同的方法(在代码中注释掉)但没有任何工作:
java.io.FileNotFoundException: /storage/emulated/0/Pictures/saved_images/image0.jpg: open failed: ENOENT (No such file or directory)
代码:
// check if ext storage is available to write
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// media available
canSaveToExternalSD = true;
// String root =
// Environment.getExternalStorageDirectory().toString();
/*
* String root = Environment.getExternalStoragePublicDirectory
* (Environment.DIRECTORY_PICTURES).toString() ;
*/
// String root =
// Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
// + File.separator + diveAlbum;
String root = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).toString()
+ "/saved_images";
File myDir = new File(root);
myDir.mkdirs();
if (!myDir.exists()) {
makeToast("Directory does not exists: " + myDir);
Log.d(TAG, "Dir does not exist!");
}
String photoName = "image" + diveNum + ".jpg";
File file = new File(root + "/" + photoName);
// File file = new File(myDir,photoName);
if (file.exists()) {
file.delete();
makeToast("Name already exists!");
}
try {
FileOutputStream out = new FileOutputStream(file);
image.compress(Bitmap.CompressFormat.JPEG, 90, out);// compress
// image fro
// output
out.flush();
out.close();
创建FileOutputStream时抛出的异常, 建议表示赞赏。 夏兰
答案 0 :(得分:1)
知道了。 已经将READ_EXTERNAL_STORAGE添加到清单而不是WRITE_EXTERNAL_STORAGE权限,没有任何2天的头部划痕无法修复。
答案 1 :(得分:0)
您可以根据需要修改此代码:
final public class FileHandler {
final private File folderCreatedDir;
final private String folderToCreate;
final private Map<String, String> mapNameToContents;
public FileHandler(File baseDirectory, String folderToCreate,
Map<String, String> mapNameToContents) {
this.folderCreatedDir = new File(baseDirectory + File.separator
+ folderToCreate);
this.folderToCreate = folderToCreate;
this.mapNameToContents = mapNameToContents;
}
private final void createFolder() {
folderCreatedDir.mkdir();
}
private final void writeMapContents() throws IOException {
Set<String> keySet = mapNameToContents.keySet();
for (String key : keySet) {
writeContents(key, mapNameToContents.get(key));
}
}
private final void writeContents(String key, String contents)
throws IOException {
File file = new File(folderCreatedDir + File.separator + key);
FileOutputStream fileOutput = new FileOutputStream(file);
if (file.canWrite()) {
fileOutput.write(contents.getBytes());
fileOutput.close();
}
}
public void writeAllContents() throws IOException {
createFolder();
writeMapContents();
}
public StringBuilder getContents(String key) throws IOException {
BufferedReader rd = new BufferedReader(new FileReader(folderCreatedDir
+ File.separator + key));
String line = "";
StringBuilder htmlBuilder = new StringBuilder();
long bytesRead = 0;
while ((line = rd.readLine()) != null) {
htmlBuilder.append(line);
bytesRead = bytesRead + line.getBytes().length + 2;
}
return htmlBuilder;
}
}
如果您使用方法,它可以帮助您将代码分解为更小的部分。此外,我建议使其更加面向对象,它将帮助您更好地可视化代码。