OutOfMemoryException while sending an image to a webserver - android

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

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.

Related

Convert Base64 encoded String into Image file back on Java Server

I want to send Image from Android to Server. I decoded image into Base64 String and send it to the server. I use following code to convert Image to String
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte [] byteArray = bao.toByteArray();
String imageToString=Base64.encodeToString(byteArray,Base64.DEFAULT);
return imageToString;
Now i am unable to convert it back to Image on server side. I tried this
byte[] imageBytes=Base64.decode(imageString);
InputStream in = new ByteArrayInputStream(imageBytes);
BufferedImage bImageFromConvert = ImageIO.read(in);
ImageIO.write(bImageFromConvert, "jpg", new File("D:\\myImage.jpg"));
i am getting Bogus Huffman table definition exception and sometime im = null exception. plz tell me what mistake i am making
Edit: Error Message javax.imageio.IIOException: Bogus Huffman table definition at this line
BufferedImage bImageFromConvert = ImageIO.read(in);
Try this
byte[] imageBytes=Base64.decode(imageString,Base64.NO_WRAP);
InputStream in = new ByteArrayInputStream(imageBytes);
Bitmap b = BitmapFactory.decodeStream(in);
Well there might be multiple issues here. The first one I think is the fact that you convert the image bytes to String (encoding them with whatever default encoding you Android environment has) and the decoding that String back to bytes without ensuring that you use the same text encoding (and thus get the same bytes).
Why not send the bytes directly? Or better yet just upload the file directly via HTTP multi-part form. There's a tutorial on this here:
http://flo.dauran.com/194-android-uploader-une-image-sur-une-serveur-web/
(it's in french, but there's detailed code examples)

Why my Image Quality is not good while sending it to server in My Android App?

This is my code to send Image on Server . my image is going to server but not in good quality.
photo = (Bitmap) data.getExtras().get("data");
image_from_camera.setImageBitmap(photo);
if(photo != null)
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 50, stream);
//Here if I increase the size of quality like up to
//photo.compress(Bitmap.CompressFormat.JPEG, 100, stream);
//then i am getting BAD REQUEST ERROR i.e request exceeds size limit.
byte[] byte_arr = stream.toByteArray();
image_string_chore_detail = Base64.encodeToString(byte_arr,Base64.DEFAULT);
System.out.println(
"IMAGE STRING IN CHORE DETAILS:: "+image_string_chore_detail);
}
So please help me what to do....
Use png-32 format for good quailty.
change the format in photoshop and by this you will improve the quailty.
Its because you are reducing the image quality to half the original in this line while compressing,
photo.compress(Bitmap.CompressFormat.JPEG, 50, stream);
The 50 in this line means half. Maybe u have to try it with 100 which will provide you full quality without any loss.
The string is too large for your server to handle. You need to change the settings in the server to allow a larger file.

Android: Upload JPG image without losing EXIF

I'm uploading JPG image as byte[] but Bitmap strips of EXIF before converting to byte[]. How do I upload raw jpg without converting it to Bitmap?
File imagefile = new File(filepath + "DSC00021.jpg");
FileInputStream fis = new FileInputStream(imagefile);
Bitmap bi = BitmapFactory.decodeStream(fis); // EXIF info lost
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPG, 100, baos);
byte[] data = baos.toByteArray();
p.s. I don't want to use any 3rd party library. ExifInterface can only write to file and not streams/byte arrays.
Above code will not work most cases. In case if you want to decode large size image you will get "out of memory error". Decode using bitmpafactory options.
Convert the file to bitmap by
Bitmap bi = BitmapFactory.decode(filepath + "DSC00021.jpg");
You can specify options too, look at API documentation
Or if you want to exchange the meta data from one file to another, sanselan will probably be the best choice. This would be much helpful when you manipulating the image, for example re-size.
The sample code will guide you in a right direction.

Android Sending Data to PHP Server

I am creating a sign up form where i need to send the following data to the PHP server to create the user account
First Name
Last Name
Email
PAssword
Image
i am sending the first four via JSON. now i am wondering how to include the image and send it to the server.
for this use the multipartentity concept.
for reference see the below code
MultipartEntity req=new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();
ByteArrayBody bab = new ByteArrayBody(data, "icon.png");
req.addPart("image", bab);
httppost.setEntity(req);
in that req.addPart("image", bab); "image" is the xml code.u sholud collect it .
you can transfer byteStream of image by HttpConnection .
i followed this link for the same .
you should go for Base64 encoding to send Image to sever.
see this link..Binary Data in JSON String. Something better than Base64
First you need to decide what kind of image you want to send. Do you want to choose an image from sd-card or take a photo with camera?
Here is very good tutorial how to do it, which even includes explanation how to implement croping of the image.
Next step, you will need to upload this file. You can get good information about that from here.

Send byte array of a Bitmap using http post in android?

In my application I need to call an API Using Http Post to send some information to the web server. So I need to append userid, name, phoneNo and image to the url. I need to convert the image to a byte array and append it to url. I used the following code but it didn't work for me. I just got a byte array like this [B#4055dd90
My code is
Bitmap bitmap1 = ((BitmapDrawable) d).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap1.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
Can Some one please help Me ?
Thanks in advance !
Two things:
Images can get pretty big. So, you may be better off using a JPG, as you can compress it (a PNG does not compress). So, you'd use Bitmap.CompressFormat.JPEG and set the amount of compression you want.
When sending in your post, you should encode the byte[] like this: Base64.encodeBase64String( bitmapdata);
Let me know if that works for you. OH, and don't forget to unencode it on your server side.
You can not send the images in the byte array form directly to server. You have to encode it into Base64 String with the help of Base64 Class. Encode Bitmap byte array to Base64 string and send that string to server with the help of HTTPPost method. If you have any doubt then you can comment.

Categories

Resources