从网址中获取多个图片

时间:2012-12-12 17:35:58

标签: android inputstream urlconnection

我有一个应用程序,允许下载3个图像并将它们显示到一个库中 为了做到这一点,我使用一个线程来下载图像和处理程序,以便将图像放入图库

问题是图像没有显示(图像视图为空)进入图库(即使我看到3张图像的3张图像)

我不知道这是因为连接速度很慢(我在手机上开发),还是因为在下载图像之前显示ui是

非常感谢您的帮助

public class ChoiceLanguage extends Activity {
    private TextView nomLangue;

    private ArrayList<Language> listeLangues;
    private ArrayList<String> listImages;
    private Gallery gallery;
    private String URL="******************";
    private Drawable mNoImage;
    Bitmap bitmap;
    ImageView imgView = null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.choice_language);


        listeLangues= getIntent().getParcelableArrayListExtra("listeLangues");
        listImages=buildListImages();


        gallery = (Gallery) findViewById(R.id.galleryImg);

        gallery.setAdapter(new AddImgAdp(this));

        gallery.setSpacing(10);


        gallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {            

                 System.out.println("URL "+ listImages.get(position) );
            }
        });


}

    private InputStream openHttpConnection(String urlString) throws IOException {
        InputStream in = null;

        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();
        conn.connect();
        in=conn.getInputStream();
        return in;
    }

    private Bitmap downloadImage( ImageView iView, String url) {
        Bitmap bitmap = null;
        InputStream in = null;
        BufferedInputStream bis ;
        try {

            in = openHttpConnection(url);
            bis= new BufferedInputStream(in, 8192);
            bitmap = BitmapFactory.decodeStream(bis);
            bis.close();
            in.close();

        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return bitmap;

    }


    private ArrayList<String> buildListImages() {
        ArrayList<String> listImg = new ArrayList<String>();
        for(Language l : listeLangues) {
            listImg.add(URL+l.getImagePath());

        }   

        return listImg;
    }

    public class AddImgAdp extends BaseAdapter {
        int GalItemBg;
        private Context cont;

        public AddImgAdp(Context c) {
            cont = c;
            TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
            GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
            typArray.recycle();
        }

        public int getCount() {
            return listImages.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {

            if (convertView == null) {
                imgView = new ImageView(cont);
            } else {
                imgView = (ImageView)convertView;
            }

            ThreadDownload progressDl= new ThreadDownload(position);
            progressDl.start();

            imgView.setLayoutParams(new Gallery.LayoutParams(150, 150));
            imgView.setScaleType(ImageView.ScaleType.FIT_XY);
            imgView.setBackgroundResource(GalItemBg);

            return imgView;
        }
    }
    private class SetImg extends Handler {

        public void handleMessage(Message msg) {

            imgView.setImageBitmap(bitmap);
        }
    }
    private class ThreadDownload extends Thread {
        SetImg img= new SetImg();
        int position ;
        public ThreadDownload(int position)
        {
            super();
            this.position=position;
        }
        public void run() {
         bitmap = downloadImage(imgView,listImages.get(position));      
         img.sendEmptyMessage(0);
        }
       }



    }

1 个答案:

答案 0 :(得分:0)

您可以使用SmartImageView,它是Android标准ImageView的直接替代品,它还允许从URL或用户的联系地址簿加载图像。图像被缓存到内存和磁盘以便超快速加载。

https://github.com/loopj/android-smart-image-view