2

I have converted an image into base64 through an online site. I went through this link to hold base64 string in a String. But i get an error saying Error:(38, 36) error: constant string too long

Please let me know how to convert base64 into an image (bitmap) in android

Community
  • 1
  • 1
Praful C
  • 152
  • 1
  • 3
  • 12

4 Answers4

4

You can just basically revert your code using some other built-in methods.

byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);

Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
Alvin Varghese
  • 760
  • 9
  • 17
3
     //encode image(from image path) to base64 string
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                Bitmap bitmap = BitmapFactory.decodeFile(pathOfYourImage);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                byte[] imageBytes = baos.toByteArray();
                String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);

     //encode image(image from drawable) to base64 string
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.yourDrawableImage);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                byte[] imageBytes = baos.toByteArray();
                String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);
chetan prajapat
  • 281
  • 1
  • 9
  • The question is how to convert base64 into an image (bitmap) in android, not the other way around. – Tushski Apr 09 '20 at 05:37
2

Did you try to use the class BitmapFactory ?

Please try something like this :

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

Also, according to the error you get, it seems that you use a static final String to hold you encoded base64 string. In Java, the length of constant string is limited to 64k.

Maxime Esprit
  • 365
  • 2
  • 20
1

First of all check your string

http://codebeautify.org/base64-to-image-converter

Try this way to convert.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView image =(ImageView)findViewById(R.id.image);

        //encode image to base64 string
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] imageBytes = baos.toByteArray();
        String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);

        //decode base64 string to image
        imageBytes = Base64.decode(imageString, Base64.DEFAULT);
        Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
        image.setImageBitmap(decodedImage);
    }
}

http://www.thecrazyprogrammer.com/2016/10/android-convert-image-base64-string-base64-string-image.html

Aditya Vyas-Lakhan
  • 13,007
  • 15
  • 58
  • 92