Bradly Spicer
Bradly Spicer

Reputation: 2318

Converting BitmapDrawable into an imageView

I have a particular scenario, I'm very close to finishing! Basically, I've got some decodeBase64 strings in a database that need to be used in a BitmapFactory and then drawn.

public static Bitmap decodeBase64(String input)
  {
    byte[] decodedByte = Base64.decode(input, 0);
    return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
  }

Here is my MainActivity.java:

    // Initiate Database
        DatabaseHandler db = new DatabaseHandler(this); // "this" refer to the context
    // Grab current Car for use.
        Car cars = db.getCurrentCar();
    // Grab String from database and decode to Base64
        //ByteConvert.decodeBase64(cars.get_image());
    // Create Bitmap object from database source
        BitmapFactory(ByteConvert.decodeBase64(cars.get_image());
    // Draw image from string
    Drawable draw = new BitmapDrawable(getResources(), BITMAPFACTORY GOES HERE);
    // Set R.id.DRAWABLE to imageView

My question, is how do I turn this string I'm returning from decodeBase64 into a BitmapFactory and then Draw it?

I have done my best to try and fill out how I think it works.

Thanks

Upvotes: 0

Views: 52

Answers (1)

Submersed
Submersed

Reputation: 8870

The Base64.decode method is returning a byte array, so you can use BitmapFactory's decodeByteArray method to build your bitmap, and set it on your ImageView.

Ex:

DatabaseHandler db = new DatabaseHandler(this);
Car cars = db.getCurrentCar();
byte[] imageData = Base64.decode(cars.get_image(), 0);
Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, new BitmapFactory.Options());
ImageView imageView = //find your image view;
imageView.setImageBitmap(bitmap);

Upvotes: 4

Related Questions