为什么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
更多推荐
发布评论