为什么Bitmap Factory在从FileInputStream obj解码流时返回null?(Why Bitmap Factory return null while decoding stre

编程入门 行业动态 更新时间:2024-10-11 03:23:00
为什么Bitmap Factory在从FileInputStream obj解码流时返回null?(Why Bitmap Factory return null while decoding stream from FileInputStream obj?)

我正在点击一个URL并将返回的图像响应保存在缓存目录中。 如果我尝试从返回的响应输入流中保存位图,那么我得到正确的位图。 现在将该响应输入流保存在缓存中并在获取之后我得到了空位图

将inputStream写入缓存目录 -

String root = mContext.getCacheDir().toString(); String path = root + "/tomorrow.jpg"; try { final File file = new File(path); final OutputStream output = new FileOutputStream(file); try { try { final byte[] buffer = new byte[1024]; int ch; while ((ch = in.read(buffer)) != -1) output.write(buffer, 0, ch); } finally { output.close(); } } catch (Exception e) { e.printStackTrace(); } }catch(Exception e){ e.printStackTrace(); }

现在我正在从缓存目录中读取文件 -

FileInputStream fin = null; try { fin = new FileInputStream(new File(path)); } catch (FileNotFoundException e) { e.printStackTrace(); } Bitmap bmp1 = BitmapFactory.decodeStream(fin);

I'm hitting an URL and saving the returned image response in cache dir. If I try to save Bitmap from Returned response inputstream then I get correct Bitmap. Now after saving that response inputstream in cache and after fetching it I'm getting null Bitmap

Write inputStream to cache dir -

String root = mContext.getCacheDir().toString(); String path = root + "/tomorrow.jpg"; try { final File file = new File(path); final OutputStream output = new FileOutputStream(file); try { try { final byte[] buffer = new byte[1024]; int ch; while ((ch = in.read(buffer)) != -1) output.write(buffer, 0, ch); } finally { output.close(); } } catch (Exception e) { e.printStackTrace(); } }catch(Exception e){ e.printStackTrace(); }

now I'm reading the file from cache dir -

FileInputStream fin = null; try { fin = new FileInputStream(new File(path)); } catch (FileNotFoundException e) { e.printStackTrace(); } Bitmap bmp1 = BitmapFactory.decodeStream(fin);

最满意答案

我要感谢Dimitri Budiansky指导我。 修复如下 -

//final byte[] buffer = new byte[1024]; //int ch; //while ((ch = in.read(buffer)) != -1) //output.write(buffer, 0, ch);

我评论过以上几行。 只需添加以下行。

bmp.compress(Bitmap.CompressFormat.PNG, 100, output);

为了澄清,您可以查看此链接

I'd like to thanks Dimitri Budiansky for guiding me. Fix as below-

//final byte[] buffer = new byte[1024]; //int ch; //while ((ch = in.read(buffer)) != -1) //output.write(buffer, 0, ch);

I commented above lines. Simply add below line.

bmp.compress(Bitmap.CompressFormat.PNG, 100, output);

for clarification u may check this Link

更多推荐

本文发布于:2023-07-29 03:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1311948.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:FileInputStream   obj   Bitmap   Factory   decoding

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!