保存到本地的方法:
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();
}