0

我有一个允许下载 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);
        }
       }



    }
4

1 回答 1

0

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

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

于 2012-12-12T17:48:45.900 回答