Android关于listview中显示网络图片的问题

在listview中第二次下载图片时就会出现

SkAndroidCodec::NewFromStream returned null


 可能是图片大了点,它第一次还没下载完就第二次开始调用了

所以我采取的措施就是:既然每次下载图片都是在子线程中执行的,于是我在外面(循环里面)等待子线程调用完毕后再进行下一张图片的下载

 以下是我 部分中的 完整代码

复制代码
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();//初始化,simpleAdapter 需要绑定的数据是List<Map<String,Object>>类型的
                        for (int i = 0; i < bitmap.length; i++) {
                            final Map<String, Object> item = new HashMap<String, Object>();
                            c.setvalue(i);    //因为需要将i变量传递到线程中,就通过一个类的变量进行获取
                            Thread t_ = new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    String url="http://"+StaticValue.add_ip+":8089/images/"+image_url[ c.getvalue()];  //网络URL
                                    bitmap[c.getvalue()] = getHttpBitmap(url);  //或获取URL转bitmap的方法
                                    savePicture(bitmap[c.getvalue()],image_url[ c.getvalue()]);//下载到本地
                                }
                            });
                            t_.start();
                            try {
                                t_.join();    //等待线程执行结束
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }

                            FileInputStream fis = null;
                            try {
                                fis = new FileInputStream(Environment.getExternalStorageDirectory().getAbsolutePath()
                                        + "/M2_test/download/" + image_url[i]);  //获取刚刚子线程中存在本机的图片
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }
                            BitmapFactory.Options op = new BitmapFactory.Options();
                            op.inPreferredConfig = Bitmap.Config.RGB_565;
                            op.inDither = true;
                            op.inSampleSize = 10;
                            op.inJustDecodeBounds = false;
                            Bitmap bitmap  = BitmapFactory.decodeStream(fis,null,op);

                            item.put("pic", bitmap);
                            item.put("mono", mono[c.getvalue()]);
                            data.add(item);

                        }
              
              //simpleAdapter 默认不支持图片的接受,所以需要重写方法 SimpleAdapter adapter
= new SimpleAdapter(getContext(), data ,R.layout.vlist, new String[]{"pic","mono"}, new int[]{R.id.im ,R.id.tv_mono}); adapter.setViewBinder(new SimpleAdapter.ViewBinder() { public boolean setViewValue(View view, Object attentionList, String textRepresentation) { if(view instanceof ImageView && attentionList instanceof Bitmap){ ImageView iv=(ImageView)view; iv.setImageBitmap((Bitmap) attentionList); return true; }else{ return false; } } }); lv.setAdapter(adapter);
复制代码

 

 

复制代码
public  Bitmap getHttpBitmap(String url)
    {
        Bitmap bitmap = null;
        try
        {
            URL pictureUrl = new URL(url);
            InputStream in = pictureUrl.openStream();
            bitmap = BitmapFactory.decodeStream(in);
            in.close();

        } catch (MalformedURLException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        }

        return bitmap;
    }

    public void savePicture(Bitmap bitmap,String pic_name)
    {
        String pictureName = Environment.getExternalStorageDirectory().getAbsolutePath()
                + "/M2_test/download/" + pic_name;
        File file = new File(pictureName);
        FileOutputStream out;
        try
        {
            out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }
复制代码

 

 

以上就是我实现在listview实现查看网络图片的代码

 

之前还遇到

Unable to decode stream: java.io.FileNotFoundException (No such file or directory)


 

我是通过 
BitmapFactory.decodeFile( UrlString, options);
怎么检查UrlString都能Logcat中显示正确的路径
所以我只能通过将网络图片下载到本机,通过获取本机路径显示图片

posted on   石墨方  阅读(2284)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示