// 将Bitmap保存为文件File的方法
private File saveAvatar(Bitmap bitmap,String filename) {
try {
String dirPath = Environment.getExternalStorageDirectory()
.getAbsolutePath();
Log.i("tag", "头像路径dirpath=="+dirPath+Constants.SAVE_GOODS_IMAGE);///storage/emulated/0
File file=new File(dirPath, filename);
fileOutputStream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, fileOutputStream);
return file;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fileOutputStream!=null){
try {
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
private File saveAvatar(Bitmap bitmap,String filename) {
try {
String dirPath = Environment.getExternalStorageDirectory()
.getAbsolutePath();
Log.i("tag", "头像路径dirpath=="+dirPath+Constants.SAVE_GOODS_IMAGE);///storage/emulated/0
File file=new File(dirPath, filename);
fileOutputStream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, fileOutputStream);
return file;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fileOutputStream!=null){
try {
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
本文介绍了一种在Android环境中将Bitmap对象保存为PNG格式文件的方法。通过使用FileOutputStream结合Bitmap的compress方法,可以将图像数据压缩并存储到指定路径下的文件中。文中详细展示了如何创建文件、写入数据及异常处理。

1546

被折叠的 条评论
为什么被折叠?



