Base64轉(zhuǎn)圖片與圖片轉(zhuǎn)Base64工具類(lèi)
沒(méi)有廢話青伤,直接上代碼
public class BitmapStringUtils {
/**
* 將bitmap轉(zhuǎn)換成為Base64
* @param bitmap
* @return
*/
public static String bitmaptoString(Bitmap bitmap) {
// 將Bitmap轉(zhuǎn)換成字符串
String string = null;
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 10, bStream);
byte[] bytes = bStream.toByteArray();
string = Base64.encodeToString(bytes, Base64.DEFAULT);
return string;
}
/**
* 將Base64轉(zhuǎn)換成為Bitmap
* @param string
* @return
*/
public static Bitmap stringtoBitmap(String string){
//將字符串轉(zhuǎn)換成Bitmap類(lèi)型
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;
}
}
使用方法
/*先得到bitmap對(duì)象*/
Bitmap headBitmap = data.getParcelableExtra("data");
RequestBody requestBody = new RequestBody(UserInfoActivity.this);
requestBody.setHeadImg(BitmapStringUtils.bitmaptoString(headBitmap));//←就像括號(hào)里面這樣用
/*上傳*/
RequestUtils.createApi().uploadHeadImg(requestBody)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new CommonsSubscriber<Response>() {
@Override
protected void onSuccess(Response response) {
Toast.makeText(UserInfoActivity.this, response.getReturnInfo(), Toast.LENGTH_SHORT).show();
getUser();
}
});