检查输入流是否被重复使用。在调用 BitmapFactory.decodeStream() 之前,需要将输入流标记为无效(即调用 InputStream.mark(0)),否则解码器将会从当前标记的位置读取流,可能导致解码失败。
示例代码:
// 从输入流中解码图片 public static Bitmap decodeStream(InputStream inputStream) { // 将输入流标记为无效 inputStream.mark(0); // 解码 bitmap Bitmap bitmap = BitmapFactory.decodeStream(inputStream); // 如果 bitmap 为 null,尝试重新解码 if (bitmap == null) { // 重置输入流到标记位置 try { inputStream.reset(); } catch (IOException e) { e.printStackTrace(); } // 尝试重新解码 bitmap bitmap = BitmapFactory.decodeStream(inputStream); } return bitmap; }
上一篇:BitmapFactory.decodeStream导致的错误信息:'D/skia:---Failedtocreateimagedecoderwithmessage'unimplemented'”