标签: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实现查看网络图片的代码
标签:ide 完整 start amp 部分 add output orm static
原文地址:https://www.cnblogs.com/Nora-F/p/9125533.html