How to read drawable bits as InputStream

前端 未结 4 1987
长情又很酷
长情又很酷 2021-01-03 08:29

There\'s say some ImageView object. I want to read bits/raw data of this object as InputStream. How to do that?

4条回答
  •  离开以前
    2021-01-03 09:09

    First get Background image of the imageview as an object of Drawable

    iv.getBackground();
    

    Then convert Drwable image into bitmap using

    BitmapDrawable bitDw = ((BitmapDrawable) d);
            Bitmap bitmap = bitDw.getBitmap();
    

    Now use ByteArrayOutputStream to get the bitmap into the stream and get bytearray[] convert bytearray into ByteArrayInputStream

    you can use the following code to get inputstream from imageview

    Full Source code

    ImageView iv = (ImageView) findViewById(R.id.splashImageView);
        Drawable d =iv.getBackground();
        BitmapDrawable bitDw = ((BitmapDrawable) d);
        Bitmap bitmap = bitDw.getBitmap();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte[] imageInByte = stream.toByteArray();
        System.out.println("........length......"+imageInByte);
        ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte);
    

    Thanks Deepak

提交回复
热议问题