So, to send an image to a web server i need to encode it to base64 and later decode on the server side.
This is what i am following.
But i get the OutOfMemoryException, at this line of code:
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
And the image size of 1mb. So, any alternate way to do this?
I read this:
Sending images using Http Post
which recommends to use MultipartEntity. But where exactly can i find those libraries? because 1 of the links is broken.
Code:
Uri selectedImageUri = data.getData();
String selectedImagePath = getPath(selectedImageUri);
try {
File imageFile = new File(selectedImagePath);
Bitmap bitmap = BitmapFactory.decodeFile(imageFile
.getAbsolutePath());
setImage.setImageBitmap(bitmap);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
base64code = Base64.encodeToString(b, Base64.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
}
Probably 1 way might be to reduce the quality. But if i set it to 20 or 30 and the image is already a low quality 1(taken from a low end device). I might get a bad quality image.
Doing something like check the size and reduce the quality depending on that. I don't think is a good idea.
Thank You