I have to upload four images from android to server using multipart entity.I am sending images by decoding it with base64.And it shows out of memory exception.I have posted the code below. Please help me fixing the bug.
private String resize(String path) {
ByteArrayOutputStream bAOS;
bitmap = BitmapFactory.decodeFile(path);
Log.i("RESIZE", "RESIZE");
// Toast.makeText(getApplicationContext(), "resizing first image",
// Toast.LENGTH_LONG).show();
String encodedImage;
byte[] imageBytes;
try {
Log.d("ActivityNewsDisplay", "inside try one");
bAOS = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bAOS);
imageBytes = bAOS.toByteArray();
encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
} catch (Exception e) {
Log.d("ActivityNewsDisplay", "inside catch one");
// TODO: handle exception
return null;
} catch (OutOfMemoryError e) {
try {
Log.d("ActivityNewsDisplay", "inside catch one");
Log.d("ActivityNewsDisplay", "inside try two");
bAOS = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 25, bAOS);
imageBytes = bAOS.toByteArray();
encodedImage = Base64
.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
} catch (OutOfMemoryError e1) {
Log.d("ActivityNewsDisplay", "inside catch two");
try {
Log.d("ActivityNewsDisplay", "inside try three");
bAOS = new ByteArrayOutputStream();
Toast.makeText(getApplicationContext(), "resize one",
Toast.LENGTH_LONG).show();
bitmap.compress(Bitmap.CompressFormat.JPEG, 10, bAOS);
imageBytes = bAOS.toByteArray();
encodedImage = Base64.encodeToString(imageBytes,
Base64.DEFAULT);
return encodedImage; // TODO: handle exception
} catch (Exception e2) {
Log.d("ActivityNewsDisplay", "inside catch three");
return null;
}
}
}
}
I am calling this function inside an asynchronous task.
@Override
protected String doInBackground(String... params) {
Log.i("inside asynchronous task", "inside task");
// TODO Auto-generated method stub
decodedImage = new String[params.length];
for (int i = 0; i < params.length; i++) {
decodedImage[i] = resize(params[i]);
}
publishProgress("progress");
return null;
}
Out of memory exception occur when the size of the image is large?