【问题标题】:Android upload image to server using base64Android使用base64上传图片到服务器
【发布时间】:2012-04-16 18:10:57
【问题描述】:

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

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;
}

【问题讨论】:

    标签: android base64 http-post image-uploading


    【解决方案1】:

    尝试此代码一次。

    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());
    }
    

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

    【讨论】:

    • 我使用相同的代码,但它仍然给我java.lang.OutOfMemoryError
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    相关资源
    最近更新 更多