public class BitmapStringUtils {
/**
* 将bitmap转换成为Base64
* @param bitmap
* @return
*/
public static String bitmaptoString(Bitmap bitmap) {
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转换成为Bitmap
* @param string
* @return
*/
public static Bitmap stringtoBitmap(String string){
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;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
使用方法
Bitmap headBitmap = data.getParcelableExtra("data")
RequestBody requestBody = new RequestBody(UserInfoActivity.this)
requestBody.setHeadImg(BitmapStringUtils.bitmaptoString(headBitmap))
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()
}
})