1. String转换成Bitmap
2. Bitmap转换成String
3. 使用示例
public Bitmap stringToBitmap(String string) {
// 将字符串转换成Bitmap类型
Bitmap bitmap = null;
try {
byte[] bitmapArray;
bitmapArray = Base64.decode(string, Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
bitmapArray.length);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
2. Bitmap转换成String
public String bitmapToString(Bitmap bitmap){
//将Bitmap转换成字符串
String string=null;
ByteArrayOutputStream bStream=new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG,100,bStream);
byte[]bytes=bStream.toByteArray();
string=Base64.encodeToString(bytes,Base64.DEFAULT);
return string;
}
3. 使用示例
public void Test(){
ImageView img = (ImageView) findViewById(R.id.img);
//首先将图片转换成bitmap,然后将bitmap转换成String
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
String bitmapString = bitmapToString(bitmap);
//将String转换成Bitmap之后,通过setImageBitmap将图片显示出来
img.setImageBitmap(stringToBitmap(bitmapString));
}