Java 将图像转换为json文件中的Base64问题

Java 将图像转换为json文件中的Base64问题,java,android,json,base64,Java,Android,Json,Base64,我在编码图像以将其发送到json文件时遇到问题。如果我将图像字符串提取到txt并解码,它工作正常,我可以看到编码的图像,但当我将其放入json文件时,当遇到“/”时,imagestring会发生变化,它会将其转换为“\/”并转换为json文件,如果imagestring更改了行,则json文件会添加“\n”。这使得不可能从json文件中提取imagestring来查看图像 ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); B

我在编码图像以将其发送到json文件时遇到问题。如果我将图像字符串提取到txt并解码,它工作正常,我可以看到编码的图像,但当我将其放入json文件时,当遇到“/”时,imagestring会发生变化,它会将其转换为“\/”并转换为json文件,如果imagestring更改了行,则json文件会添加“\n”。这使得不可能从json文件中提取imagestring来查看图像

ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
  Bitmap bitmap2 = ((BitmapDrawable) secondImageV.getDrawable()).getBitmap();
  bitmap2.compress(Bitmap.CompressFormat.JPEG, 100, baos2);
  byte[] imageBytes2 = baos2.toByteArray();
  imageString2 = Base64.encodeToString(imageBytes2, Base64.DEFAULT);

 sendPost(imageString1,imageString2,sendAlert);

public void sendPost(String im1, String im2, String alert) {

        JSONObject json = new JSONObject();
        try {
            json.put("name", compname.getText());
            json.put("hydrovalue", _hydrometerValue.getText());
            json.put("im1", im1) ;
            json.put("im2", im2);
            json.put("hydrohealth", check1);
            json.put("hydrorepair", check2);
            json.put("hydroalert", alert);
            json.put("stime", stime.getText());
            json.put("etime", etime.getText());
            json.put("comm", _comments.getText());

        } catch (JSONException e) {
            e.printStackTrace();
        }

        String url = "https://api.myjson.com/bins/lmz8i";
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, json,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Toast.makeText(getApplicationContext(), "String Response : " + response.toString(), Toast.LENGTH_LONG).show();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), "Error getting response", Toast.LENGTH_LONG).show();
            }
        });
ByteArrayOutputStream=newbytearrayoutputstream();
位图bitmap2=((BitmapDrawable)secondImageV.getDrawable()).getBitmap();
bitmap2.compress(Bitmap.CompressFormat.JPEG,100,2);
字节[]imageBytes2=2.toByteArray();
imageString2=Base64.encodeToString(imageBytes2,Base64.DEFAULT);
sendPost(imageString1、imageString2、sendAlert);
公共void sendPost(字符串im1、字符串im2、字符串警报){
JSONObject json=新的JSONObject();
试一试{
put(“name”,compname.getText());
put(“hydrowalue”,_hydrometerValue.getText());
json.put(“im1”,im1);
json.put(“im2”,im2);
json.put(“hydrohealth”,检查1);
json.put(“hydrorepair”,check2);
put(“hydroalert”,alert);
put(“stime”,stime.getText());
put(“etime”,etime.getText());
put(“comm”,_comments.getText());
}捕获(JSONException e){
e、 printStackTrace();
}
字符串url=”https://api.myjson.com/bins/lmz8i";
JsonObjectRequest JsonObjectRequest=新的JsonObjectRequest(Request.Method.POST,url,json,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
Toast.makeText(getApplicationContext(),“字符串响应:+Response.toString(),Toast.LENGTH_LONG).show();
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Toast.makeText(getApplicationContext(),“获取响应时出错”,Toast.LENGTH_LONG.show();
}
});

使用Base64“URL安全”编码。实例化这些编码器/解码器如下java.util.Base64.getUrlEncoder和java.util.Base64.getUrlDecoder

感谢快速响应伙伴!我尝试了Base64.URL\u安全编码,尽管它超过了符号“\/”问题是,当我尝试通过多个在线解码器网站将其解码为图像时,它不会显示我的图像。这就是它现在返回的方式:,但在这种格式下,我无法将其解码为原始图像…我在这个api上看不到任何格式良好的base64。myjson.com/bins/fz4qe似乎在不同的base64之间存在\n,但无论如何,使用它我无法获得任何url.Marco Lettieri感谢您的信息伴侣!!在将其添加到json文件之前,我通过在imagestring:imageString2=IIImageString2.replaceAll(\\r\\n | \\r | \\n“,”);处使用replaceAll方法解决了我的问题。
Base64.encodeToString(imageBytes2,Base64.NO\u-WRAP);
应该处理意外的换行。如果值在json文本中被双引号引用,
/
(可能还有
+
)的转义没有意义。转义只对非标准ID有意义。