使用Base64编码器从活动转换图库照片时,出现错误的Base64字符串。使用网站进行测试时,它是损坏的图像。尝试发布此字符串时,我的ExpressAPI也会给我一个字符串验证错误。
我已经尝试使用不同的位图变体,例如Factory等。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//call super
super.onActivityResult(requestCode, resultCode, data);
Uri imageUri = data.getData();
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(imageUri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
encodedString = encodeImage(selectedImage);
}
private String encodeImage(Bitmap bm)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG,100,baos);
byte[] b = baos.toByteArray();
String encImage = Base64.encodeToString(b, Base64.DEFAULT);
Log.d(encImage, "Image:");
return encImage;
}
我显然希望使用Base64字符串,但是我得到了:
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB: AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAONA1wDASIA
AhEBAxEB/8QAHwAAAQQDAQEBAQAAAAAAAAAAAAQFBgcICQoBAgML/8QAhRAAAAEHBwgGBQYHBg4M
CQEZAQACAwQFESEGBzFBUWHwCBRxgZGhscEJEhMk0eEKFSM08Ro5UpKXuBYiM0RXctgXMkNUgtIY
GSU4OkJid3iIorK3wjZWWFlkaHOVlrTU4iY1dHV2ttXW1ygqN1VmlJintbPyJ0VGR0hTZXmFhoeo
xMbH/8QAGwEBAAMBAQEBAAAAAAAAAAAAAAQFBgEDAgf/xABIEQACAAIGBwYEBQMDAgUEAwEAAQQR
ITFBUWGRAhRxgaHB8BKCorHR4QWS0vEiMkJSYgMGchWywhY1JDRUVfIjJUXiE0NTc//aAAwDAQAC EQMRAD8A7+CCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCAC
CCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACC
CCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCC
CACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCC
ACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCA
CCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCAC
CCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACC
CCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCC
CACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCC
ACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCA
CCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCAC
CCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACC
CCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCC
CACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCCACCCC
ACCCCACCCCACCCCACCCCACCCCACCCCACCCCAqOdOdGSEzM2s4M7U4zTMk3Iia6R0opwpYtmHVZcn
pKMMZSyjSC+IgahRif1QcCURE3rGiACGlHoTZZZSuVlJ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V0v7jEoZOhIubZBmqyKNsJkyM9VTpE5ydTAxcSChTJFzal
0ZGW/JnpBsjCafKVZmYqUoJTM31XOfJ9nOdJedCTIIglXJw4x7xORHmgcAg4wE
查看此请求:request
这是请求:
private void uploadMeme() {
RequestQueue requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
String url = "Hiding this for personal reasons";
final MemeCreateActivity activity = (MemeCreateActivity) getActivity();
assert activity != null;
try {
JSONObject jsonBody = new JSONObject();
jsonBody.put("Title", title);
jsonBody.put("Longtitude", activity.getLon());
jsonBody.put("Lattitude", activity.getLat());
jsonBody.put("Picture", activity.getEncodedString());
final String mRequestBody = jsonBody.toString();
Log.d(mRequestBody, "Request:");
JsonObjectRequest stringRequest = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
activity.nextView();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
parseVolleyError(error);
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
//Sets Request headers
Map<String, String> headers = new HashMap<>();
headers.put("Accept", "application/json");
headers.put("Content-Type", "application/json");
headers.put("Authorization", "Bearer " + activity.getUser().getJwt());
return headers;
}
@Override
public byte[] getBody() {
try {
return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
return null;
}
}
};
requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
}
}
答案 0 :(得分:1)
Java使用base64字符串中的空格对位图进行解码,请尝试以下操作:encodedString.replaceAll("\\s+","");
答案 1 :(得分:0)
尝试一下:
public String encodeTobase64(Bitmap image) {
Bitmap immagex=image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);
Log.e("EncodedImage: ", imageEncoded);
return imageEncoded;
}
在onActivityResult()中使用以下代码
InputStream imageStream = null;
try {
imageStream = this.getContentResolver().openInputStream(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
encodeTobase64(yourSelectedImage);
答案 2 :(得分:0)
您可以尝试通过选择的文件的路径获取位图:
在您的onActivityResult中
Uri targetUri = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(targetUri,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
//getting the actual path of the file
String path = cursor.getString(columnIndex);
cursor.close();
Bitmap bm = BitmapFactory.decodeFile(path);
//your encoding function is ok
encodedString = encodeImage(selectedImage);
答案 3 :(得分:0)
对。。我找到了答案。基本上,Android认为在我的字符串中插入多个空格很有趣。
这样做,您可以将其全部删除:
activity.getEncodedString().replaceAll("\s+","")