Android加载图片时出错,提示“SKImagedecoder::Factory returned null”,请问是什么原因?
Android加载图片时出错,提示“SKImagedecoder::Factory returned null”,请问是什么原因?

发表者:{dwah3456}dwah3456 1-11 [ 1]

 分享到:
 
 
回答(第1条,共1条,通过审核的共1条)

  这个不是从sd卡读图片产生的问题,从sd卡读图片用 Bitmap bitmap = BitmapFactory.decodeFile(imagePath)或者Drawable.createFromPath(imagePath)都是没有问题的。你从SD卡获取时出错的这张图片,是之前你从服务器获取图片保存的;问题就出在你从服务器获取图片保存到SD卡时。

  InputStream is = bufferedHttpEntity.getContent();
  //有可能你在这儿已经读取过一次is,此时InputStream流的起始位置已经被移动过了,当你在下面再次读取InputStream时会出错,也就是说,你保存到SD卡上的文件是错误的,所以从SD卡读取这个文件时自然会出错。
  bitmap = BitmapFactory.decodeStream(is);

  也有人说是HttpURLConnection的问题,如果使用apache的HttpClient的话,就不会有问题了。
  HttpGet httpRequest = new HttpGet(url);
  HttpClient httpclient = new DefaultHttpClient();
  HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
  HttpEntity entity = response.getEntity();
  BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(entity);
  InputStream is = bufferedHttpEntity.getContent();
  bitmap = BitmapFactory.decodeStream(is);

  但不管怎样,重复读取InputStream肯定会出错,据说可以用is.reset()重置InputStream,你可以试一下。
  另外最好是先用InputStream流保存文件到SD卡,然后再到SD卡获取Bitmap(BitmapFactory.decodeFile(imagePath))或者Drawable(Drawable.createFromPath(imagePath));这样就不用两次读取InputStream了。

回答评级:★★★☆☆☆☆☆☆ 回答者:{51kl}51kl 1-13

回答
相关问题:
我是:
本站注册用户
用户名: 密码:
非本站注册用户
我来回答:

回答字数在8000字以内。回答即可得2分,回答被采用后,根据评级可得到相应的积分和智慧果。
比如:回答评级为5,可得150分和15个智慧果。

 

 
 
 
 
 
 
































 

© 版权所有  2001-2024 知识网站
Copyright (C) 2001-2024 allzhishi.com All rights reserved
联系我们 — 电话:15973023232  微信:zhishizaixian  ICP备案号:湘ICP备08003211号-4