我使用這段代碼保存在外部存儲位圖,但如果它不存在,它不會創建該文件夾:保存位圖在Android作爲JPEG在外部存儲設備的文件夾中
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOutputStream = null;
File file = new File(path + "/Captures/", "screen.jpg");
try {
fOutputStream = new FileOutputStream(file);
capturedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOutputStream);
fOutputStream.flush();
fOutputStream.close();
MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
return;
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
return;
}
我如何保存新目錄中的圖像是否存在,如果文件夾存在於設備中,則保存默認值?
file.getParentFile()。mkdirs() – njzk2
看看如何做到這一點在的AsyncTask http://stackoverflow.com/a/29795857/3496570 – Nepster