Android關於listview中顯示網狀圖片的問題

來源:互聯網
上載者:User

標籤:ide   完整   start   amp   部分   add   output   orm   static   

在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實現查看網狀圖片的代碼

Android關於listview中顯示網狀圖片的問題

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.