Reputation: 31
Hello guys I am working on a project that uploads image file in the server. I am sending a data to the server using Volley
. The Data that I will be sending contains base64
string. I encountered OutOfMemoryError when I decoded an image file. Am I implementing it correctly? Here is my code:
for (int i = 0; i < pathList.size(); i++) {
//error occurred here
Bitmap image = BitmapFactory.decodeFile(pathList.get(i), options);
Log.d("bitmapImage", "nisudSiya" + " " + image);
encodedImage = UploadImageHelper.encodeImageBitmap(UploadImageHelper.scaleBitmap(image, 1000, 1000));
stringUri.add(encodedImage);
}
StringRequest stringRequest = new StringRequest(Request.Method.POST, POST_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(CreatePostActivity.this, response.toString(), Toast.LENGTH_SHORT).show();
VolleyApp.getInstance().cancelPendingRequests(PUSH_MULTIPLE_IMAGES);
mProgressUpload.dismiss();
finish();
Log.d("tyler", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Toast.makeText(CreatePostActivity.this, "Error Uploading!", Toast.LENGTH_SHORT).show();
mProgressUpload.dismiss();
Log.d("tagsdsad23", error.toString());
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
for (int i = 0; i < stringUri.size(); i++) {
Attachment attachment = new Attachment();
attachment.setType("image");
attachment.setUrl(stringUri.get(i));
shit.add(attachment);
}
Log.d("shitsize", String.valueOf(shit.size()));
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < shit.size(); i++) {
try {
JSONObject jsonObject = new JSONObject();
Log.d("shittype", shit.get(i).getUrl());
jsonObject.put("type", shit.get(i).getType());
jsonObject.put("url", shit.get(i).getUrl());
jsonArray.put(jsonObject);
Log.d("tyler-gwapa", jsonObject.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
Log.d("tyler-gwapa", jsonArray.toString());
Map<String, String> params = new HashMap<String, String>();
params.put(USER_TOKEN, userToken);
params.put(CAPTION, postContent.getText().toString());
params.put(ATTACHMENT, jsonArray.toString());
return params;
}
};
VolleyApp.getInstance().addToRequestQueue(stringRequest, PUSH_MULTIPLE_IMAGES_TAG);
Upvotes: 1
Views: 96
Reputation: 1428
Try the following to compress bitmap and encode it.
Bitmap selectedImage = getResizedBitmap(yourBitmaptoCompress, 350, 350);
Define getResizedBitmap method
public Bitmap getResizedBitmap(Bitmap image, int bitmapWidth, int
bitmapHeight) {
return Bitmap.createScaledBitmap(image, bitmapWidth, bitmapHeight,
true);
}
Encode image bitmap (base64)
String encodedImage = encodeImage(selectedImage);
Define encodeImage Method
private String encodeImage(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);
byte[] b = baos.toByteArray();
String encImage = Base64.encodeToString(b, Base64.DEFAULT);
return encImage;
}
Upvotes: 1
Reputation: 913
compress image before you send to server ...!
image .compress(Bitmap.CompressFormat.JPEG, 100, outStream);
i hope its helpful to you ...!
Upvotes: 0