在ImageView中设置位图图像

时间:2018-07-02 05:54:23

标签: java android bitmap imageview base64

我已经尝试过了

try {
        byte[] decodedString = Base64.decode(repPlus, Base64.URL_SAFE);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        holder.imageView.setImageBitmap(decodedByte);
    } catch (Exception e) {
        Log.d("Error: ", e.getMessage());
    }

它返回一个错误:

bad base-64

然后我尝试了此Bad base-64 error。并且它消除了错误。还有Base64 to Bitmap to display in ImageView用于解码base64字符串

try {
        String repSlash = product.getImage().replace("/", "_");
        String repPlus = repSlash.replace("+", "-");
        byte[] decodedString = Base64.decode(repPlus, Base64.URL_SAFE);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        holder.imageView.setImageBitmap(decodedByte);
    } catch (Exception e) {
        Log.d("Error: ", e.getMessage());
    }

但是图像无法在应用程序中查看。

1 个答案:

答案 0 :(得分:1)

初始化位图 decodedByte

后尝试执行此操作
BitmapDrawable drawable = new BitmapDrawable(getResources(), decodedByte);
holder.imageView.setBackgroundDrawable(drawable);

编辑:尝试以下操作:

String base64Image = product.getImage().split(",")[1];

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

holder.imageView.setImageBitmap(decodedByte);

希望有帮助!