下载图片并将其加载到listitem

时间:2017-02-22 08:37:24

标签: android image listview url download

我是Android的新手,我在应用程序上工作。我想下载一个图像并在列表项中显示它。我使用了很多功能。我的代码运行没有错误,但图像不会显示在我的代码的屏幕上。

public loader(Context context, String[] img) {
        super(context, R.layout.item, img);

        this.context = context;
        this.img = img;

        inflater = LayoutInflater.from(context);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // ImageView imgshow = (ImageView) findViewById(R.id.image1);

        if (null == convertView) {
            convertView = inflater.inflate(R.layout.item, parent, false);

        }
        ImageView imageView = (ImageView) convertView.findViewById(R.id.imageview);
        //first method
        //  Glide.with(context)
        // .load(img[position])
        //.into(imageView);
        //second method
        DownloadImageTask download=new DownloadImageTask((ImageView) convertView.findViewById(R.id.imageview));
        download.execute("http://previews.123rf.com/images/faysalfarhan/faysalfarhan1402/faysalfarhan140200008/25989999-Back-left-arrow-icon-glossy-purple-button-Stock-Photo.jpg");
        //new DownloadImageTask((ImageView) convertView.findViewById(R.id.imageview))
        //.execute(img[position]);
        //third method
        //Picasso.with(context).load(img[position]).into(imageView);
        //Log.e("image_url","https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/Square_200x200.svg/1024px-Square_200x200.svg.png");
        return convertView;





    }
    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;
        TextView t;


        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = "http://previews.123rf.com/images/faysalfarhan/faysalfarhan1402/faysalfarhan140200008/25989999-Back-left-arrow-icon-glossy-purple-button-Stock-Photo.jpg";

            Bitmap mIcon11 = null;
            // Toast.makeText(mainpage.this ,urldisplay ,Toast.LENGTH_SHORT).show();

            TextView t=(TextView)findViewById(R.id.texttt);
            // t.setText(urldisplay);

            try {


                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                 Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);


        }
    }


}

我把它放在我的列表中使用(img是字符串数组我在其中保存url并显示它们但是为了测试我将它替换为loader类中的一个图像url)

 String[] img = new String[1000];
    loader im=new loader(mainpage.this,img);

    lv.setAdapter(im);

4 个答案:

答案 0 :(得分:0)

您无法直接在ImageView中设置图像。你必须在Bitmap中转换该图像,然后设置

public static Bitmap getBitmapFromURL(String src) {
    try {
        Log.e("src",src);
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        Log.e("Bitmap","returned");
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        Log.e("Exception",e.getMessage());
        return null;
    }
}

然后设置为imageview:

imageView.setImageBitmap(getBitmapFromURL(url));

试试希望它可以帮助你

答案 1 :(得分:0)

使用piccasso。

要使用piccasso添加它,请添加

compile 'com.squareup.picasso:picasso:2.5.2'

到您的应用级别gradle。和 使用这个

Picasso.with(context).load("http://previews.123rf.com/images/faysalfarhan/faysalfarhan1402/faysalfarhan140200008/25989999-Back-left-arrow-icon-glossy-purple-button-Stock-Photo.jpg").into(imageview);

或者你的getCount()方法返回0。 (或添加它)将其更改为

@Override
    public int getCount() {
        return img.length;
    }

答案 2 :(得分:0)

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

答案 3 :(得分:0)

使用Glide

glide的依赖:compile&#39; com.github.bumptech.glide:glide:3.7.0&#39;

并添加代码

db-server-preprod*