我正在使用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上传图片到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!