How to read drawable bits as InputStream

前端 未结 4 2000
长情又很酷
长情又很酷 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:13

    You can use the drawing cache to retrieve a Bitmap representation of any View class.

    view.setDrawingCacheEnabled(true);
    Bitmap b = view.getDrawingCache();
    

    Then you can write the bitmap to an OutputStream, for example:

    b.compress(CompressFormat.JPEG, 80, new FileOutputStream("/view.jpg"));
    

    In your case I think you can use a ByteArrayOutputStream to get a byte[] from which you can create an InputStream. The code would be something like this:

    ByteArrayOutputStream os = new ByteArrayOutputStream(b.getByteCount());
    b.compress(CompressFormat.JPEG, 80, os);
    byte[] bytes = os.toByteArray();
    

提交回复
热议问题