【发布时间】:2017-02-01 10:19:51
【问题描述】:
我尝试保存位图,但outStream = new FileOutputStream(file); 行总是抛出错误java.io.FileNotFoundException: /storage/emulated/0/inpaint/card.png: open failed: ENOENT (No such file or directory)。
//Save bitmap
InputStream inputStream = null;
try {
inputStream = getContentResolver().openInputStream(uri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap bmp = BitmapFactory.decodeStream(inputStream);
File f3=new File(Environment.getExternalStorageDirectory()+"/inpaint/");
if(!f3.exists())
f3.mkdirs();
OutputStream outStream = null;
File file = new File(Environment.getExternalStorageDirectory() + "/inpaint/"+"card"+".png");
try {
outStream = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, outStream);
outStream.close();
Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
我也加了
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
到清单。
【问题讨论】:
-
您的设备是否运行在 Android MM 或更高版本?
-
我的目标是 API 15 及更高版本。
-
好的,但是运行您的应用程序的设备的 android 操作系统版本是什么?如果它是 >= marshmallow 那么你必须在运行时请求许可。
-
f3.mkdirs();。检查返回值,因为它可能无法创建目录。在这种情况下,显示一个 toast 告诉用户这一点。并返回。不要继续编写代码,因为尝试在不存在的目录中创建文件毫无意义。