Byte array of image into imageview

后端 未结 2 1339
谎友^
谎友^ 2020-12-13 09:43

I\'ve searched a bit but can\'t get a clear glimpse of it. How can I set a byte array of an Image into an ImageView? I tried with this but it didn\'t work.

相关标签:
2条回答
  • 2020-12-13 09:59

    This is how you can convert a Bitmap to a ByteArray and a ByteArray to a Bitmap:


    Convert Bitmap to ByteArray:

    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    

    Convert ByteArray to Bitmap:

    Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    ImageView image = (ImageView) findViewById(R.id.imageView1);
    image.setImageBitmap(Bitmap.createScaledBitmap(bmp, image.getWidth(), image.getHeight(), false));
    
    0 讨论(0)
  • 2020-12-13 10:11

    Get Bytes From Bitmap

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
        byte[] bytearray = stream.toByteArray();
        return bytearray;
    
    0 讨论(0)
提交回复
热议问题