【问题标题】:how to upload image in base64 on server如何在服务器上上传base64格式的图片
【发布时间】:2014-11-24 17:12:59
【问题描述】:

我有一个问题,我正在将图像上传到服务器上,但事实并非如此。我已经在 base64 中转换图像并通过 json。但是 json 没有正确关闭,因为我遇到了错误。错误 id om postimafe 变量。在这个变量{"key"""encode,这里是json没有关闭。

        // code for convert base64

        public static String getBase64String(String baseFileUri)
            {
                String encodedImageData  = "";
                try
                {
                    System.out.println("getBase64String method is called :" +baseFileUri);
                    Bitmap bm = BitmapFactory.decodeFile(baseFileUri);
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
                    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object   
                    byte[] b = baos.toByteArray();
                    encodedImageData  = Base64.encodeToString(b, Base64.DEFAULT);
                    //ArrayList<NameValuePair> imagearraylistvalue = new ArrayList<NameValuePair>();
                    //imagearraylistvalue.add(new BasicNameValuePair("image", encodedImage));

                    System.out.println("encode data in upload file :" +encodedImageData );
                }
                catch(Exception ex)
                {
                    System.out.println("Exception in getBase64String method in Utility class :" +ex);
                }
                return encodedImageData ;
            }

        // code for json and uplod base64 to server but i m getting error

        System.out.println("fullupload image for 1:" +fulluploadimgpath);
    String base64String = Utility.getBase64String(fulluploadimgpath);
    System.out.println("base64String is in :" +base64String);
    if (base64String != null) 
{
JSONObject postImageData = new JSONObject();

postImageData.put("media",base64String);

 System.out.println("post image :" +postImageData);
HttpResponse imgPostResponse = Utility.postDataOnUrl(Utility.getBaseUrl()+"user/upload",obj.toString());
System.out.println("fullupload image for imgPostResponse:" +imgPostResponse);

     if (imgPostResponse != null)
 {

String imgResponse = Utility.readUrlResponseAsString(imgPostResponse);
System.out.println("imgResponse is in imgResponse :" +imgResponse);
if (imgResponse != null|| imgResponse.trim().length() != 0)
                                                    {
                                                        JSONObject jResObj = new JSONObject();
                                                            if (jResObj.getBoolean("rc"))
                                                            {
                                                            obj.put(hidobj.getReceiveAs(),jResObj.getLong("ident"));
                                                        }

}

【问题讨论】:

  • 你有没有出错?
  • Json 未关闭...我收到错误,
  • 09-30 12:51:15.462: I/System.out(670): "exception": "Server exception"
  • 十二月9日至30日:51:15.032:I /的System.out(670):后的图像:{ “媒体”:“\ / 9J \ / 4AAQSkZJRgABAQAAAQABAAD \ / 2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB \ nAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH \ / 2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB \ nAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH \ / wAARCAAiAEEDASIA \ nAhEBAxEB \ / 8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL \ / 8QAtRAAAgEDAwIEAwUFBAQA \ nAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3 \ nODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1d 跨度>
  • 看到这个json没有关闭

标签: android json eclipse base64


【解决方案1】:
String encodedImageData =getEncoded64ImageStringFromBitmap(your bitmap);

public String getEncoded64ImageStringFromBitmap(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 70, stream);
    byte[] byteFormat = stream.toByteArray();
    // get the base 64 string
    String imgString = Base64.encodeToString(byteFormat, Base64.NO_WRAP);

    return imgString;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-16
    • 2022-12-07
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多