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);
Related
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
I have the code below to decode a bitmap to a base64 string.
for(String e:paths)
{
String usepath=e.replace("%", "//");
Bitmap m=BitmapFactory.decodeFile(usepath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
m.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String bb= Base64.encodeToString(b, Base64.NO_WRAP);
Log.e("Photo", bb);
String usepath prints like
/mnt/sdcard/DCIM/Camera/IMG_20140424_132023.jpg
I have saved the image on my pc and used an online tool to decode it to base64 and i got a long string of around 650kb(after uploading to google app engine) yet the string i get using the above code is like 10% of that and does not display the image .
But i can use the same image path to set an image view and it works like below
Bitmap bm= BitmapFactory.decodeFile(usepath);
holder.imageItem.setImageBitmap(bm);
Any reasons why the base64 encoding failing?
Ronald
Try adding the flag Base64.URL_SAFE to the encoding method.
Consider also that if the image is too large you may not get all the bytes you need in the String (you may try writing to a temp file previous to send the content).
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
I need to send image as a string parameter in webservices to server. Just like we send string parameter in webservices. For this I am converting image into string as follows
Resources r = this.getResources();
Bitmap bm = BitmapFactory.decodeResource(r, R.drawable.icon);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 50, baos); //bm is the bitmap object
byte[] image = baos.toByteArray();
String encodedImage = Base64.encodeToString(image,Base64.DEFAULT);
Now I need to send this string encodedImage like this.
calling Url-- http://pdtrptr/asfsdf/services/add.php?file=encodedImage
My question is how can we send this string image along with url --like above url
Is server coding for receiving string image differs for android compare to iphone because (after sending the image from android to server, when trying to get from server i am getting null value), where as iphone mobile is able to send and receive the image.
Thanks
We can try the code which is providing in following link...
Image Upload with HttpPost
In my android application, i want to convert a jpeg image to base64 binary encoded value.
How to acheive this in android ?
Edit
I have encoded my bitmap image into base64 string by following code:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
byte[] val = stream.toByteArray();
String ba = Base64.encodeToString(val, Base64.DEFAULT);
The output is in the form of string. BUt I need the output as binary value.
if using KSOAP2 for connectivity use this
new MarshalBase64().register(envelope); this is do the magic.