我尝试发送从相机捕获的位图,如下面的代码:
Object[] params = (Object[]) ev.data;
String userId = (String) params[0];
String accessKey = (String) params[1];
Bitmap bitmap = (Bitmap) params[2];
String url = getResources().getString(R.string.url_base) + getResources()
.getString(R.string.url_send_photo) + userId + ""
+ "/photo?type=ID_CARD";
OkHttpClient client = getNewHttpClient();
MediaType image = MediaType.parse("image/png");
RequestBody body = RequestBody.create(image, AppUtil.bitmapToBase64String(bitmap));
Request req = new Request.Builder().url(url)
.addHeader("X-Bayarind-Access", accessKey)
.addHeader("Content-Type", "image/png")
.addHeader("Content-Transfer-Encoding", "base64")
.post(body).build();
Response res = client.newCall(req).execute();
if (res.isSuccessful()) {
// DO SOMETHING
} else {
String resp = res.body().string();
log.info(">>> RESP: " + resp);
}
方法 AppUtil.bitmapToBase64String 如下所示:
public static String bitmapToBase64String(Bitmap bmp) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
return Base64.encodeToString(byteArray, Base64.DEFAULT);
}
方法 getNewHttpClient()如下所示
private OkHttpClient getNewHttpClient() throws Exception {
TrustManager[] trustManagers = {new CustTrustManager()};
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustManagers, new SecureRandom());
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.sslSocketFactory(sslContext.getSocketFactory());
return builder.build();
}
我总是收到以下错误:
"status":500,"error":"Internal Server Error","exception":"java.lang.IllegalArgumentException","message":"Illegal base64 character a"
有人能指出我哪里犯了错误吗?