1

Below is the portion of code I used to encode the image file from bitmap to base64 string by import android.util.Base64 in my android project: `

Bitmap img_bmp=BitmapFactory.decodeStream(getContentResolver().
openInputStream(this.browseImageURI));

ByteArrayOutputStream baos = new ByteArrayOutputStream();  
img_bmp.compress(Bitmap.CompressFormat.JPEG, 30, baos); 
byte[] image = baos.toByteArray();
 String profile_img = Base64.encodeToString(image, Base64.DEFAULT);

` The string profile_img will be saved in mysql database as a string. When I retrieve the string value from database, I will decode image string from String to Bitmap by using the below code:

`

Intent i = getIntent(); //pass the value from previous activity
str_img= i.getStringExtra("img");
img_bm = StringToBitMap(str_img);
imgview = (ImageView)findViewById(R.id.imageView1);
imgview.setImageBitmap(img_bm); // display the image

//Function to convert string to bitmap
    public Bitmap StringToBitMap(String image){
           try{
               byte [] encodeByte=Base64.decode(image,Base64.DEFAULT);
               Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
               return bitmap;
             }catch(Exception e){
               e.getMessage();
              return null;
             }
     }

` I expected it will display the image, however the image is not displayed and I get a log message from the Base64 decoder "--- decoder->decode returned false".

Can someone help me to figure out what is wrong in my code?

And do anyone know how to convert the base64 string image (pass from JSON to php script) into a blob format so that I can store it as BLOB in mysql. Thank you in advance.

Charles
  • 50,943
  • 13
  • 104
  • 142
Student
  • 561
  • 1
  • 9
  • 16

0 Answers0