<tfoot id='LTr5k'></tfoot>

<i id='LTr5k'><tr id='LTr5k'><dt id='LTr5k'><q id='LTr5k'><span id='LTr5k'><b id='LTr5k'><form id='LTr5k'><ins id='LTr5k'></ins><ul id='LTr5k'></ul><sub id='LTr5k'></sub></form><legend id='LTr5k'></legend><bdo id='LTr5k'><pre id='LTr5k'><center id='LTr5k'></center></pre></bdo></b><th id='LTr5k'></th></span></q></dt></tr></i><div id='LTr5k'><tfoot id='LTr5k'></tfoot><dl id='LTr5k'><fieldset id='LTr5k'></fieldset></dl></div>

<small id='LTr5k'></small><noframes id='LTr5k'>

    1. <legend id='LTr5k'><style id='LTr5k'><dir id='LTr5k'><q id='LTr5k'></q></dir></style></legend>

        • <bdo id='LTr5k'></bdo><ul id='LTr5k'></ul>

        Android使用base64上传图片到服务器

        时间:2024-04-14
        <i id='ffaFt'><tr id='ffaFt'><dt id='ffaFt'><q id='ffaFt'><span id='ffaFt'><b id='ffaFt'><form id='ffaFt'><ins id='ffaFt'></ins><ul id='ffaFt'></ul><sub id='ffaFt'></sub></form><legend id='ffaFt'></legend><bdo id='ffaFt'><pre id='ffaFt'><center id='ffaFt'></center></pre></bdo></b><th id='ffaFt'></th></span></q></dt></tr></i><div id='ffaFt'><tfoot id='ffaFt'></tfoot><dl id='ffaFt'><fieldset id='ffaFt'></fieldset></dl></div>
        • <tfoot id='ffaFt'></tfoot>
            <bdo id='ffaFt'></bdo><ul id='ffaFt'></ul>

              1. <small id='ffaFt'></small><noframes id='ffaFt'>

                  <tbody id='ffaFt'></tbody>

                  <legend id='ffaFt'><style id='ffaFt'><dir id='ffaFt'><q id='ffaFt'></q></dir></style></legend>
                  本文介绍了Android使用base64上传图片到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用base64将图像上传到服务器,如果图像很小,它工作正常,但如果图像很大,它会给我内存输出异常

                  试试{ByteArrayOutputStream 流 = 新的 ByteArrayOutputStream();bitmap.compress(Bitmap.CompressFormat.PNG, 100, 流);bitmap.recycle();byte[] byteArray = stream.toByteArray();流.close();流=空;字符串 ba1 = Base64.encodeToString(byteArray, Base64.DEFAULT);位图=空;System.gc();//立即释放位图字节数组 = 空;System.gc();HttpClient httpclient = new DefaultHttpClient();HttpPost httppost = new HttpPost(Data.upload_photo);JSONObject jsonObj = 新 JSONObject();jsonObj.put("user_id", LoginParser.id);int cnt = ba1.length()/4;System.out.println("cnt=>" + cnt);jsonObj.put("图像", ba1);jsonObj.put("标题", edt_caption.getText().toString());//创建 POST 对象并添加参数/** StringEntity 实体 = 新 StringEntity(jsonObj.toString(),* HTTP.UTF_8);System.out.println(实体);* entity.setContentType("应用程序/json");* httppost.setEntity(实体);** HttpResponse 响应 = httpclient.execute(httppost);*/StringEntity se = new StringEntity(jsonObj.toString());//se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,//"应用程序/json"));se.setContentType("应用程序/json");httppost.setEntity(se);HttpResponse 响应 = httpclient.execute(httppost);字符串结果 = EntityUtils.toString(response.getEntity());System.out.println("=>=>" + 结果);JSONObject jsonObject = 新 JSONObject(结果);if (jsonObject.getString("status").equals("success"))返回成功";别的返回 jsonObject.getString("message");} 捕捉(IOException e){e.printStackTrace();} 捕捉(JSONException e){//TODO 自动生成的 catch 块e.printStackTrace();}返回空值;}

                  解决方案

                  试试这段代码.

                  ByteArrayOutputStream bao = new ByteArrayOutputStream();bm.compress(Bitmap.CompressFormat.JPEG, 90, bao);字节[] ba = bao.toByteArray();字符串 ba1 = Base64.encodeBytes(ba);数组列表名称值对 >nameValuePairs = 新数组列表名称值对 >();nameValuePairs.add(new BasicNameValuePair("image", ba1));尝试 {HttpClient httpclient = new DefaultHttpClient();HttpPost httppost = 新的HttpPost("网址字符串");httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));HttpResponse 响应 = httpclient.execute(httppost);HttpEntity 实体 = response.getEntity();是 = entity.getContent();//Toast.makeText(SignUpActivity.this, "加入失败", Toast.LENGTH_SHORT).show();} 捕捉(异常 e){Log.e("log_tag", "http 连接错误" + e.toString());}

                  <块引用>

                  如果您有任何疑问,我可以帮助您.

                  I am uploading image to server using base64 its working fine if image is small but if image is big it gives me memory out exception

                  try {
                          ByteArrayOutputStream stream = new ByteArrayOutputStream();
                          bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                          bitmap.recycle();
                          byte[] byteArray = stream.toByteArray();
                          stream.close();
                          stream = null;
                          String ba1 = Base64.encodeToString(byteArray, Base64.DEFAULT);
                  
                          bitmap = null;
                          System.gc(); // immediately free bitmap
                  
                          byteArray = null;
                          System.gc();
                          HttpClient httpclient = new DefaultHttpClient();
                          HttpPost httppost = new HttpPost(Data.upload_photo);
                  
                          JSONObject jsonObj = new JSONObject();
                          jsonObj.put("user_id", LoginParser.id);
                          int cnt = ba1.length() / 4;
                          System.out.println("cnt=>" + cnt);
                          jsonObj.put("image", ba1);
                  
                          jsonObj.put("caption", edt_caption.getText().toString());
                          // Create the POST object and add the parameters
                  
                          /*
                           * StringEntity entity = new StringEntity(jsonObj.toString(),
                           * HTTP.UTF_8); System.out.println(entity);
                           * entity.setContentType("application/json");
                           * httppost.setEntity(entity);
                           * 
                           * HttpResponse response = httpclient.execute(httppost);
                           */
                  
                          StringEntity se = new StringEntity(jsonObj.toString());
                          // se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                          // "application/json"));
                          se.setContentType("application/json");
                          httppost.setEntity(se);
                          HttpResponse response = httpclient.execute(httppost);
                  
                          String result = EntityUtils.toString(response.getEntity());
                          System.out.println("=>=>" + result);
                          JSONObject jsonObject = new JSONObject(result);
                          if (jsonObject.getString("status").equals("success"))
                              return "success";
                          else
                              return jsonObject.getString("message");
                      } catch (IOException e) {
                          e.printStackTrace();
                      } catch (JSONException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
                      return null;
                  }
                  

                  解决方案

                  Try this code once.

                  ByteArrayOutputStream bao = new ByteArrayOutputStream();
                  bm.compress(Bitmap.CompressFormat.JPEG, 90, bao);
                  byte[] ba = bao.toByteArray();
                  String ba1 = Base64.encodeBytes(ba);
                  ArrayList < NameValuePair > nameValuePairs = new
                  ArrayList < NameValuePair > ();
                  nameValuePairs.add(new BasicNameValuePair("image", ba1));
                  try {
                      HttpClient httpclient = new DefaultHttpClient();
                      HttpPost httppost = new
                      HttpPost("URL STRING");
                      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                      HttpResponse response = httpclient.execute(httppost);
                      HttpEntity entity = response.getEntity();
                      is = entity.getContent();
                      //Toast.makeText(SignUpActivity.this, "Joining Failed", Toast.LENGTH_SHORT).show();
                  } catch (Exception e) {
                      Log.e("log_tag", "Error in http connection " + e.toString());
                  }
                  

                  I can help you if you have any doubt.

                  这篇关于Android使用base64上传图片到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何将文件转换为 Base64? 下一篇:Swift base64 解码返回 nil

                  相关文章

                  <tfoot id='pr86W'></tfoot>

                    • <bdo id='pr86W'></bdo><ul id='pr86W'></ul>

                  1. <i id='pr86W'><tr id='pr86W'><dt id='pr86W'><q id='pr86W'><span id='pr86W'><b id='pr86W'><form id='pr86W'><ins id='pr86W'></ins><ul id='pr86W'></ul><sub id='pr86W'></sub></form><legend id='pr86W'></legend><bdo id='pr86W'><pre id='pr86W'><center id='pr86W'></center></pre></bdo></b><th id='pr86W'></th></span></q></dt></tr></i><div id='pr86W'><tfoot id='pr86W'></tfoot><dl id='pr86W'><fieldset id='pr86W'></fieldset></dl></div>
                  2. <legend id='pr86W'><style id='pr86W'><dir id='pr86W'><q id='pr86W'></q></dir></style></legend>

                      <small id='pr86W'></small><noframes id='pr86W'>