Android的上传图片使用服务器的base64 [英] Android upload image to server using base64

查看:152
本文介绍了Android的上传图片使用服务器的base64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Base64其工作正常,如果图像是小,但如果图像是大它给了我记忆出异常图像上传到服务器

 尝试{
        ByteArrayOutputStream流=新ByteArrayOutputStream();
        bitmap.com preSS(Bitmap.Com pressFormat.PNG,100,流);
        bitmap.recycle();
        字节[]的字节数组= stream.toByteArray();
        stream.close();
        流= NULL;
        字符串BA1 = Base64.en codeToString(字节数组,Base64.DEFAULT);

        位= NULL;
        System.gc()的; //立即释放位图

        的字节数组= NULL;
        System.gc()的;
        HttpClient的HttpClient的=新DefaultHttpClient();
        HttpPost httppost =新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(实体);
         *
         * Htt的presponse响应= httpclient.execute(httppost);
         * /

        StringEntity本身=新StringEntity(jsonObj.toString());
        // se.setContentEncoding(新BasicHeader(HTTP.CONTENT_TYPE,
        //应用/ JSON));
        se.setContentType(应用/ JSON);
        httppost.setEntity(SE);
        HTT presponse响应= httpclient.execute(httppost);

        字符串结果= EntityUtils.toString(response.getEntity());
        的System.out.println(=> =>中+导致);
        的JSONObject的JSONObject =新的JSONObject的(结果);
        如果(jsonObject.getString(状态)。等于(成功))
            返回成功;
        其他
            返回jsonObject.getString(信息);
    }赶上(IOException异常E){
        e.printStackTrace();
    }赶上(JSONException E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }
    返回null;
}
 

解决方案

试试这个code一次。

  ByteArrayOutputStream宝=新ByteArrayOutputStream();
bm.com preSS(Bitmap.Com pressFormat.JPEG,90,宝);
byte []的BA = bao.toByteArray();
字符串BA1 = Base64.en codeBytes(BA);
ArrayList的<的NameValuePair> namevaluepairs中=新
ArrayList的<的NameValuePair> ();
nameValuePairs.add(新BasicNameValuePair(形象,BA1));
尝试 {
    HttpClient的HttpClient的=新DefaultHttpClient();
    HttpPost httppost =新
    HttpPost(URL字符串);
    httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
    HTT presponse响应= httpclient.execute(httppost);
    HttpEntity实体= response.getEntity();
    是= entity.getContent();
    //Toast.makeText(SignUpActivity.this,加入失败,Toast.LENGTH_SHORT).show();
}赶上(例外五){
    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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆