setImageDrawable不适用于URL

时间:2016-09-09 11:11:55

标签: android url imageview drawable

我想将Url中的图像设置为imageView。为了测试我拍了一些照片到drawable文件夹。

//creat a list of images and put images inside
    final int[] imageList = new int[]{R.drawable.ic_test_img_1, R.drawable.ic_test_img_2, R.drawable.ic_test_img_3,
            R.drawable.ic_test_img_4, R.drawable.ic_test_img_5, R.drawable.ic_test_img_6};

    //for each picture into the list...
    for (int i = 0; i < imageList.length; i++) {

        //set RelativeLayout
        final RelativeLayout relView = new RelativeLayout(this);
        //set Params for the RelativeLayout
        relView.setLayoutParams(new RelativeLayout.LayoutParams((windowWidth - 80), windowHeight - 80));
        relView.setX(40);
        relView.setY(40);
        relView.setTag(i);
        //set BackgroundColor RelativeLayout
        relView.setBackgroundColor(Color.WHITE);

        //set ImageView
        ImageView img = new ImageView(this);
        //set the image from the list into the imageView
        img.setImageResource(imageList[i]);
        //set params to RelativLayout
        img.setLayoutParams(new RelativeLayout.LayoutParams((windowWidth), windowHeight));
        //show image
        relView.addView(img);

这很有效。现在我想从URL的ArrayList中设置图像。我写了这段代码:

//for each picture into the list...
    for (int i = 0; i < imageUrl.size(); i++) {

        //set RelativeLayout
        final RelativeLayout relView = new RelativeLayout(this);
        //set Params for the RelativeLayout
        relView.setLayoutParams(new RelativeLayout.LayoutParams((windowWidth - 80), windowHeight - 80));
        relView.setX(40);
        relView.setY(40);
        relView.setTag(i);
        //set BackgroundColor RelativeLayout
        relView.setBackgroundColor(Color.WHITE);

        //set ImageView
        final ImageView img = new ImageView(this);
        //set the image from the list into the imageView

        final String url = imageUrl.get(i);

        new Thread() {

            public void run () {
                URL myUrl = null;
                try {
                    myUrl = new URL(url);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
                InputStream inputStream = null;
                try {
                    inputStream = (InputStream)myUrl.getContent();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Drawable drawable = Drawable.createFromStream(inputStream, null);
                img.setImageDrawable(drawable);
            }
        }.start();

        //set params to RelativLayout
        img.setLayoutParams(new RelativeLayout.LayoutParams((windowWidth), windowHeight));
        //show image
        relView.addView(img);

此代码无效。 imageView中没有图片。该守则有什么问题?

2 个答案:

答案 0 :(得分:0)

您应该使用Glide或任何其他图像加载库,并保存所有头痛

并且它就像那样容易

Glide.with(this).load("http://googl.com/gEgYUd").into(imageView);

答案 1 :(得分:0)

你需要在Thread中使用runOnUiThread方法,它会更新imageview

this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Drawable drawable = Drawable.createFromStream(inputStream, null);
                img.setImageDrawable(drawable);
            }
        });