簡體   English   中英

將BitmapDrawable轉換為imageView

[英]Converting BitmapDrawable into an imageView

我有一個特殊的場景,我即將完成! 基本上,我在數據庫中有一些decodeBase64字符串,需要在BitmapFactory中使用它們然后進行繪制。

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

這是我的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

我的問題是,如何將我從decodeBase64返回的字符串轉換為BitmapFactory,然后繪制它?

我已盡力嘗試填寫我認為的工作方式。

謝謝

Base64.decode方法返回一個字節數組,因此您可以使用BitmapFactory的encodeByteArray方法來構建位圖,並將其設置在ImageView上。

例如:

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM