/** * 图片转成string * * @param bitmap * @return */
public static String convertIconToString(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();// outputstream
bitmap.compress(CompressFormat.PNG, 100, baos);
byte[] appicon = baos.toByteArray();// 转为byte数组
return Base64.encodeToString(appicon, Base64.DEFAULT);
}
/** * string转成bitmap * * @param st */
public static Bitmap convertStringToIcon(String st) {
// OutputStream out;
Bitmap bitmap = null;
try {
// out = new FileOutputStream("/sdcard/aa.jpg");
byte[] bitmapArray;
bitmapArray = Base64.decode(st, Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
// bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
return bitmap;
} catch (Exception e) { return null; } }
从preference中去除图片填充到ImageView中
Bitmap photo=convertStringToIcon(preferences.getString("photo", "无法获取"));//将图片从String类型转换成Bitmap
Drawable drawable = new BitmapDrawable(photo);
dt_info_head_photo.setImageDrawable(drawable);
等比例缩放图片
public static Bitmap zoomImg(Bitmap bm, int newWidth ,int newHeight){
// 获得图片的宽高
int width = bm.getWidth();
int height = bm.getHeight();
// 计算缩放比例
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
ELog.e("====newbm====="+newbm.getHeight());
return newbm;
}
Java学习笔记——File类之文件管理和读写操作、下载图片(借鉴学习参考文章)
http://blog.youkuaiyun.com/jueblog/article/details/9429953