0

I'm new to android, I work on an app. I want to download an image and show it in list item. I used many functions for that. My code runs without error but image doesn't display on screen that's part of my code.

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);


        }
    }


}

I put that in my list using (img is array of strings I save urls in it and display them but for testing I replace it by one image url inside loader class)

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

    lv.setAdapter(im);
haider_kazal
  • 3,448
  • 2
  • 20
  • 42
Shorouk Adel
  • 127
  • 3
  • 20

4 Answers4

0

you can't set image in ImageView directly. you have to convert that image in Bitmap and then set

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;
    }
}

and then set to imageview :

imageView.setImageBitmap(getBitmapFromURL(url));

try this hope it helps you

Zaki Pathan
  • 1,762
  • 1
  • 13
  • 26
0

Use piccasso.

To use piccasso to add it add

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

To your app level gradle. And use this

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);

or maybe your getCount() method is returning 0. (or add it) change it to

@Override
    public int getCount() {
        return img.length;
    }
Kiran Benny Joseph
  • 6,755
  • 4
  • 38
  • 57
0
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Akshay Chopde
  • 670
  • 4
  • 10
0

Use Glide

dependency for glide: compile 'com.github.bumptech.glide:glide:3.7.0'

and add the code

ImageView imageView = (ImageView) findViewById(R.id.my_image_view);

  Glide.with(this).load("https://s3-us-west-1.amazonaws.com/powr/defaults/image-slider2.jpg").into(imageView);
bhaskar kurzekar
  • 238
  • 2
  • 14