zoukankan      html  css  js  c++  java
  • 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中显示正确的路径
    所以我只能通过将网络图片下载到本机,通过获取本机路径显示图片
  • 相关阅读:
    Python修改文件内容
    Python实现用户注册到文件
    Postman接口测试
    Linux下安装LoadRunner LoadGenerator
    Loadrunner参数化避免重复数据
    Ta-Lib用法介绍 !
    迭代器 生成器
    深入理解python多进程编程
    python多进程
    python多线程
  • 原文地址:https://www.cnblogs.com/Nora-F/p/9125533.html
Copyright © 2011-2022 走看看