5
Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.image); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object final String encodedImage = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT); 

This is my code. It uploads the image onto the server. However, All I can see is a box. Where am I going wrong?

2
  • 3
    Possible duplicate of How to convert a image into Base64 string? Commented Mar 16, 2016 at 9:16
  • It's better to post your server side code as well. Commented Mar 16, 2016 at 9:57

2 Answers 2

2

Make sure you convert back to bitmap at the End, ie Server side

1, convert your imageview to bitmap.

 imageView.buildDrawingCache(); Bitmap bmap = imageView.getDrawingCache(); 

2, convert bitmap to base64 String and pass to server

public String getEncoded64ImageStringFromBitmap(Bitmap bitmap) { ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.JPEG, 70, stream); byte[] byteFormat = stream.toByteArray(); // get the base 64 string String imgString = Base64.encodeToString(byteFormat, Base64.NO_WRAP); return imgString; } 

3, At server side convert base64 to bitmap. (this is java code, do it in your server side language)

byte[] decodedString = Base64.decode(Base64String.getBytes(), Base64.DEFAULT); Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 

set this bitmap to display image

Sign up to request clarification or add additional context in comments.

3 Comments

I tried with Base64.NO_WRAP also. Still it's not working.
No I checked that too. I used an online Base64 converter and used that string. It worked. Something's going wrong with encoding is my guess.
are you getting the same string in server side ? use some log and conform its not the problem with your POST method,
0

convert your image to bitmap.

 File imagefile = new File(Environment.getExternalStorageDirectory(),"/image/test.jpg" ); FileInputStream fis = null; try { fis = new FileInputStream(imagefile); } catch (FileNotFoundException e) { logger.error(Log.getStackTraceString(e)); e.printStackTrace(); } Bitmap bm = BitmapFactory.decodeStream(fis); 

Bitmap to Byte[]

 ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 10 , baos); byte[] img = baos.toByteArray(); 

Byte[] to String:

String s= Base64.encodeToString(img , Base64.DEFAULT) 

8 Comments

when i do this, I am getting a nullpointerexception on bm.compress
our variable bm is null. As it's reached , you'll either have to debug/log there and cross check imagefile
Do I have to give the entire path for the image?
ya, u need to give complete path eg. File imagefile = new File(Environment.getExternalStorageDirectory(),"/image/test.jpg" ); cross check file exist or not
Yeah I gave the complete path and made sure the image is present there. I don't know why the nullpointer exception still.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.