读取网络图片和本地sd卡图片文件 解析为空 SkImageDecoder::Factory returned null

来源:互联网 发布:安卓pad应用推荐 知乎 编辑:程序博客网 时间:2024/05/01 19:53

1.sd卡读取图片解析返回null对象(图片实际是存在的sd卡)

最近在使用BitmapFactory.decodeByteArray读取本地图片有时会发生读取的图片是null(联想手机一些横拍照片),保存在本地后,通过字节流读取图片文件再转换为图片,可能会的报SkImageDecoder::Factory returned null的log日志导致解析不成功返回null对象

解决方法:BitmapFactory.decodeFile(picfileString,opts)代替上面方法直接根据地址读取转换为图片不再出现空!

2.网络读取字节流转换图片返回null对象(读取的字节流长度是有的)

  错误方法:

   BitmapFactory.decodeStream(conn.getInputStream());直接把网络数据流转换成图片(图片大小有限制,大图可能出现解析不成功返回为null)

   BitmapFactory.decodeByteArray();把conn.getInputStream()转换成字节流作为参数传入解析成图片也可能会出现空

 解决方法:把网络数据流转换文件,存储在本地再通过BitmapFactory.decodeFile(picfileString,opts)读取!

                       

/ /把图片存到本地                                                                 File file=new File(path);    InputStream inputStream=conn.getInputStream();    FileOutputStream outputSteam=new FileOutputStream(file);    byte[] buffer=new byte[2048];    int readbyte=0;    while((readbyte=inputStream.read(buffer))!=-1)    {    outputSteam.write(buffer, 0, readbyte);    }outputSteam.close();inputStream.close();                          //读取图片                          BitmapFactory.Options opts = new BitmapFactory.Options();       int scale=1;//默认缩放比例,1代表不缩放    opts.inJustDecodeBounds = true ;//不获取图片,只是拿到图片信息    Bitmap bitmap=BitmapFactory.decodeFile(picPath opts);    int w = opts.outWidth;    int h = opts.outHeight;                //计算长宽缩放比例(这里120表示要显示的长宽)    double scalew=opts.outWidth/120.0;    double scaleh=opts.outHeight/120.0;    scale=(int)Math.sqrt(Double.valueOf((scalew*scalew+scaleh*scaleh)));    if(scale==0){scale=1;}else{//取2的倍数缩放if(scale%2==0){}else{scale=scale+1;}}    opts.inJustDecodeBounds = false ;//确认缩放比例后开始准备加载合适的缩放比图片    opts.inSampleSize = scale;     opts.inPreferredConfig=Config.RGB_565;    //bitmap=BitmapFactory.decodeByteArray(b, 0, b.length, opts);    bitmap=BitmapFactory.decodeFile(MyUtils.getTempPic()+clipPicName, opts);

                                             
0 0
原创粉丝点击