Image Converted to Byte not shown when Converting back to Image - android

In my android application , i want to convert image into bytes and encode it. and send to database. But when i convert it back on image, it do not show. Please help.and tell me where i am making mistake
final Bitmap image=(images.get(position));
int size = image.getRowBytes() * image.getHeight();
ByteBuffer buffer = ByteBuffer.allocate(size); //Create a new buffer
image.copyPixelsToBuffer(buffer); //Move the byte data to the buffer
byte[] array = buffer.array();
encodedImageString = Base64.encodeToString(array, Base64.DEFAULT);
Now on the server side when i decode this encoded imageString and write it, it do not display image.
Byte[] imageByteArary= base64.decode(encodedImageString);
File myfile=new File("D://test1.jpg");
myfile.createNewFile();
FileOutputStream fOut=new FileOutputStream (myfile);
fOut.write(imageByteArray);
fOut.close();

I was facing the same problem.The String that you send from client side is not same what you receive at server side.
Check this for solution

Related

Storing Image in VCard and reading from it in android code manually

I am creating vCard files from my android app.
I am storing data manually(without using any libray)
I am able to write the data,read and parse it in my app.But when I save Image .I have gor two issues.
1)I am not able to save the image captured using camera..which throws an Out of memoery exception while writing the base 64 encoded string into vcard.
2)I am able to save the base 64 encoded string of some image which I took from gallery,but while reading it doesn't get me image.I am reding all the data from vCard line by line and base64 encoded string is not coming as a single line.(Please note that I stored each value to the file using \r\n)
Please let me know the proper way of doing this.
Code Snippets
Encoding
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
encodedProfileImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
Writing
fw = new FileWriter(vcfFile);
...
fw.write("PHOTO;ENCODING=BASE64;TYPE=PNG:"+encodedProfileImage + "\r\n");
Reading and decoding
else if(strline[0].equals("PHOTO;ENCODING=BASE64;TYPE=PNG")){
String imagestr=strline[1];
byte[] byteArray = Base64.decode(imagestr, Base64.DEFAULT);
card.profileImage = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
}
When reading it in you can use BitmapFactory.Options
http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html
Use the decodeByteArray call that takes BitmapFactory.Options as the last argument.
if you set inSampleSize = 2
It will reduce the size of the incoming image to something managable.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2; //Scale it down
options.inPreferredConfig = Bitmap.Config.RBG_565; // Less memory

How To Decode The Server Response of image String Uploaded through Base64?

I am uploading my image on server using Base64 as-
private String convertBitmapToString(ImageView imgview) {
imgview.buildDrawingCache();
Bitmap objbmap = imgview.getDrawingCache();
ByteArrayOutputStream objbitmapArrayStream = new ByteArrayOutputStream();
objbmap.compress(Bitmap.CompressFormat.JPEG, 90, objbitmapArrayStream);
byte[] objbitmaparray = objbitmapArrayStream.toByteArray();
String img_string = Base64.encodeBytes(objbitmaparray);
return img_string;
}
And When I want to Show That Image Hit Again Php Server Which give me response as-
"picture":"\/9j\/4AAQSkZJRgABAQAAAQABAAD\/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT\/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT\/wAARCABGAEcDASIAAhEBAxEB\/8QAHwAAAQUBAQEBAQE"
I am unable to decode it please suggest .
Well server gives you base64 encoded picture obviously, because online base64 decoder showed this when i decoded your string:
�JFIF�������C�
which is a sign of a correct image content, so just decode it.
BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(encodedBytes);

Send image to server through httpmultipart

I am making an android app in which i am sending images from gallery to server through xml..Any type of help will be appreciated... hanks
This is how I handle it in my application:
// bitmap is your Bitmap object
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// The next line should be adjust to use the format and compression you want.
bitmap.compress(CompressFormat.PNG, 0, stream);
byte[] byteArr = stream.toByteArray();
// The next line would be where you write the byte array to your xml structure:
myXml += Base64.encodeBase64String( byteArr );
In my application, the byte[] gets saved to the db as a blob prior to the xml structure being created. So this code isn't exactly what I'm doing. But it should give you the idea.

Image conversion from JPEG to Byte Array in Android

I want to convert JPEG image to Byte Array in android. I am using the below code:
if (PhotoScreen.st_bitPicture != null)
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
boolean b = PhotoScreen.st_bitPicture.compress(CompressFormat.JPEG, 100, bos);
Log.w("test2", "BOOLEAN BOOLEAN BOOLEAN BOOLEAN :"+b);
m_base64EncodedImage = Base64.encodeToString(bos.toByteArray(), Base64.DEFAULT);
}
But it is compressing the image. How can i do without compressing the image?
When you say "compressing", do you mean "convert it to base 64?". You already have a byte array, and then you encode it in base 64. What is your desired output?
PhotoScreen.st_bitPicture.compress(CompressFormat.JPEG, 100, bos);
Creates an in-memory JPEG image (JPEG compressed).
bos.toByteArray()
Creates a byte array with the JPEG data. That's what you want.
Base64.encodeToString(bos.toByteArray(), Base64.DEFAULT);
Encodes this data to base 64. Just omit that step if you don't want base 64. If you want any other output (like a string with the byte array converted to hexadecimal), then do that instead.

How to display image from blob data in android sqlite?

I have been trying to store image form android sdcard to the sqlite database. And it worked fine. The image is getting stored into the database as blob. This is the rough code I have been using for that.
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
imgView.setImageBitmap(bitmap);
int size = bitmap.getWidth() * bitmap.getHeight();
ByteArrayOutputStream out = new ByteArrayOutputStream(size);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();}
byte[] x = out.toByteArray();
In the database side, the code is like this
ContentValues values = new ContentValues();
byte[] bytes = null;
bytes = img_bytes.getBytes();
values.put("IMAGE", bytes);
And for displaying I have used the code as below.
ImageView image = new ImageView(this);
byte[] img_bytes = result.get("IMAGE").getBytes();
Bitmap bMap = BitmapFactory.decodeByteArray(img_bytes, 0, img_bytes.length);
image.setImageBitmap(bMap);
When I printed the string value of the byate array, it was getting like [B#44da2db0. But the byte array is not getting displayed into image.
Somebody please help me with this.
Thanks
I have found a solution. Thanks to all.
I have used base64 encoding method.
ie; For displaying the image, I have been using a hash map to get the result first of all from the database table.
So before saving into the map, I have encoded the bytes to string using base 64 method.
Then On the Activity side for displaying the image, I have decoded back the string using base 64 again and then converted to bytes and the image got displayed.

Categories

Resources