0

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

Community
  • 1
  • 1
Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226
  • Have a look at this [link](http://stackoverflow.com/questions/5416038/uploading-image-to-server-in-multipart-along-with-json-data-in-android) – Abhay Kumar Dec 15 '12 at 07:16

1 Answers1

1

Upload images to server with MultipartEntity is one of the good option.

MultipartEntity libraries Downloads :

apache-mime4j-0.6.jar

httpmime-4.0.1.jar

Hope it helps you.

Thanks.

Pratik Sharma
  • 13,307
  • 5
  • 27
  • 37