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?

Gowtham Bk
  • 61
  • 1
  • 1
  • 6
  • 3
    Possible duplicate of [How to convert a image into Base64 string?](http://stackoverflow.com/questions/4830711/how-to-convert-a-image-into-base64-string) – Mangesh Mar 16 '16 at 09:16
  • It's better to post your server side code as well. – BobGao Mar 16 '16 at 09:57

2 Answers2

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

Joseph Joseph
  • 751
  • 1
  • 7
  • 13
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)
  • when i do this, I am getting a nullpointerexception on bm.compress – Gowtham Bk Mar 16 '16 at 09:57
  • our variable bm is null. As it's reached , you'll either have to debug/log there and cross check imagefile –  Mar 16 '16 at 10:03
  • Do I have to give the entire path for the image? – Gowtham Bk Mar 16 '16 at 10:04
  • ya, u need to give complete path eg. File imagefile = new File(Environment.getExternalStorageDirectory(),"/image/test.jpg" ); cross check file exist or not –  Mar 16 '16 at 10:06
  • Yeah I gave the complete path and made sure the image is present there. I don't know why the nullpointer exception still. – Gowtham Bk Mar 16 '16 at 10:08
  • Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference at UploadActivity.onCreate(UploadActivity.java:53) – Gowtham Bk Mar 16 '16 at 10:16
  • the bitmap is basically null. Don't know how to resolve it – Gowtham Bk Mar 16 '16 at 10:17
  • Unable to decode stream: java.io.FileNotFoundException: /sdcard/Download/kali_vpn_configuration_picateshackz.jpg: open failed: EACCES (Permission denied) – Gowtham Bk Mar 16 '16 at 11:02