您的位置:首页 > 移动开发 > Android开发

Android保存图片到本地

2016-02-25 14:48 489 查看

保存到本地的方法:

private boolean saveImage(byte[] data){
if(TextUtils.isEmpty(mUrl) || data == null)
return false;
boolean save = false;
String path = DIR_IMAGE + mUrl.hashCode();
FileOutputStream fos = null;
try {
File imageDir = new File(DIR_IMAGE);
if (!imageDir.exists()) {
imageDir.mkdirs();
}
fos = new FileOutputStream(path);
fos.write(data);
save = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

return save;
}


private boolean saveImage1(Bitmap bitmap){
if(TextUtils.isEmpty(mUrl) || bitmap == null)
return false;
boolean save = false;
String path = DIR_IMAGE + mUrl.hashCode();
FileOutputStream fos = null;
try {
File imageDir = new File(DIR_IMAGE);
if (!imageDir.exists()) {
imageDir.mkdirs();
}
fos = new FileOutputStream(path);
bitmap.compress(Bitmap.CompressFormat.PNG,100,fos);
save = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

return save;
}


将Bitmap装成byte[]

public byte[] compressBitmap(Bitmap bitmap) {
if (bitmap == null) {
return null;
}
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100, stream);
return stream.toByteArray();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: