http://hiphotos.baidu.com/yyq0125/pic/item/53616050a142b7320df3e352.jpg
这张图片(不能显示的图片中的一张),显示在一个Gallery中,但是始终显示不出来,不知道为什么
贴下代码:public class HandlerThreadTest extends Activity {

private String[] myURLs = {"http://hiphotos.baidu.com/stonecando/pic/item/a4889f0ff03b06de37d122ad.jpg",
"http://hiphotos.baidu.com/maozehongbd/pic/item/0417494248ee6c486963e570.jpg",
"http://hiphotos.baidu.com/tl196605/pic/item/dc746cdb7e219ffdb7fd48a6.jpg",
"http://hiphotos.baidu.com/maozehongbd/pic/item/0417494248ee6c486963e570.jpg",
"http://hiphotos.baidu.com/maozehongbd/pic/item/0417494248ee6c486963e570.jpg",
"http://hiphotos.baidu.com/maozehongbd/pic/item/0417494248ee6c486963e570.jpg",
"http://hiphotos.baidu.com/maozehongbd/pic/item/0417494248ee6c486963e570.jpg",
"http://hiphotos.baidu.com/maozehongbd/pic/item/0417494248ee6c486963e570.jpg",
"http://hiphotos.baidu.com/maozehongbd/pic/item/0417494248ee6c486963e570.jpg",
"http://hiphotos.baidu.com/maozehongbd/pic/item/0417494248ee6c486963e570.jpg",
"http://hiphotos.baidu.com/18kb/pic/item/c2a65d27314db90ec9955924.jpg",
"http://hiphotos.baidu.com/lao135798010/pic/item/d79c23d26d1e63393af3cfd1.jpg",
"http://hiphotos.baidu.com/lenovode/pic/item/1c2a66c4b70c242e9c163d84.jpg",
"http://hiphotos.baidu.com/lenovode/pic/item/1c2a66c4b70c242e9c163d84.jpg",
"http://hiphotos.baidu.com/lenovode/pic/item/1c2a66c4b70c242e9c163d84.jpg",
"http://hiphotos.baidu.com/lenovode/pic/item/1c2a66c4b70c242e9c163d84.jpg",
"http://hiphotos.baidu.com/lenovode/pic/item/1c2a66c4b70c242e9c163d84.jpg",
"http://hiphotos.baidu.com/lenovode/pic/item/1c2a66c4b70c242e9c163d84.jpg",
"http://hiphotos.baidu.com/lenovode/pic/item/1c2a66c4b70c242e9c163d84.jpg",
"http://hiphotos.baidu.com/lenovode/pic/item/1c2a66c4b70c242e9c163d84.jpg",
"http://hiphotos.baidu.com/275166424/pic/item/8264fc1932a26fd6ae51330a.jpg",
"http://hiphotos.baidu.com/yyq0125/pic/item/53616050a142b7320df3e352.jpg",
};
private ArrayList<Bitmap> imgList=new ArrayList<Bitmap>();
private ImageView iv;
private Gallery gallery;
private Handler handler;
private ProgressBar loadProgressBar;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        HandlerThread handlerThread = new HandlerThread("handler_thread");
        handlerThread.start();
        handler = new Handler();
        updateUI();
    }
    
    public void updateUI(){
     Thread t = new Thread(){ @Override
public void run() {
// TODO Auto-generated method stub
setBitmapFromWebsite(myURLs);
handler.post(new Runnable(){
public void run(){
gallery = (Gallery)findViewById(R.id.mobs);
gallery.setAdapter(new ImageAdapter(HandlerThreadTest.this));
//loadProgressBar = (ProgressBar)findViewById(R.id.loadProgressBar);
System.out.println("size=" + imgList.size());
//loadProgressBar.setEnabled(false);
}
});
}
    
     };
     t.start();
    }
    /**
     * @param urls 图片网络地址数组
     * @return 下载图片并存放在imgList中
     * */
    private void setBitmapFromWebsite(String[] urls){
imgList.clear();
for(int i = 0 ; i < urls.length ; i ++){

try {
URL url = new URL(urls[i]);
URLConnection conn = url.openConnection();
//conn.setRequestProperty("X-Online-Host", "hiphotos.baidu.com");
conn.connect();
InputStream in = conn.getInputStream();
            BufferedInputStream bis= new BufferedInputStream(in);
            Bitmap bm = BitmapFactory.decodeStream(bis);
            
            imgList.add(bm);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(urls[i] + e.getMessage());
e.printStackTrace();
}
}
}
    
    public class ImageAdapter extends BaseAdapter {   
        public ImageAdapter(Context c) {   
            mContext = c;   
        }   
        public int getCount() {   
            return imgList.size();   
        }   
        public Object getItem(int position) {   
            return position;   
        }   
        public long getItemId(int position) {   
            return position;   
        }   
        public View getView(int position, View convertView, ViewGroup parent) {   
            ImageView i = new ImageView(mContext);   
            i.setImageBitmap(imgList.get(position));
            System.out.println("set image imaList[" + position + "]");
            i.setAdjustViewBounds(true);   
            i.setLayoutParams(new Gallery.LayoutParams(   
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));   
            return i;   
        }   
        private Context mContext;   
    }
}

解决方案 »

  1.   


    URL url = new URL(urls[i]);
                    URLConnection conn = url.openConnection();
                    //conn.setRequestProperty("X-Online-Host", "hiphotos.baidu.com");
                    conn.connect();
                    InputStream in = conn.getInputStream();
                    BufferedInputStream bis= new BufferedInputStream(in);
                    Bitmap bm = BitmapFactory.decodeStream(bis);
                    
                    imgList.add(bm);原因是这块儿,bm == null,所以存到List里面的是空值,为什么呢?有的能下载,有的又不能下载,这些链接都能打开啊?
      

  2.   

    哥哥们,找到原因了, InputStream in = conn.getInputStream();打开了没关,估计是满了,下一张图片读不出来,垃圾回收后又能重新读,所以有的能读,有的不能读
      

  3.   

    Android's decoders do not currently support partial data on decode. i.e. if the complete 
    data stream is not available, then they return failure. I will investigate trying to support 
    this in the future.
    找到原因了,补下