0

I'm trying to create a wallpaper application to display a range of images based on a drawable folder. How can I get this images from URL, i.e., to say get image by url instead?

public class ImageAdapter extends BaseAdapter {

    private Context mContext;

    public Integer[] mThumbIds = {
        R.drawable.wallpaper_1,
        R.drawable.wallpaper_2,
        R.drawable.wallpaper_3,
        R.drawable.wallpaper_4,
        R.drawable.wallpaper_5,
        R.drawable.wallpaper_6,
        R.drawable.wallpaper_7,
        R.drawable.wallpaper_8,
        R.drawable.wallpaper_9,
        R.drawable.wallpaper_10
    };

    public ImageAdapter(Context c) {
        mContext = c;
    }

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

    @Override
    public Object getItem(int position) {
        return mThumbIds[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(imageView.getScaleType().CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT, 400));
        return imageView;
    }

}
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
MrMR
  • 279
  • 6
  • 16

2 Answers2

0

Lets say your image urls are something like http://myweb.com/images/1.jpg and it's counting like 2.jpg, 3.jpg and ...

Now you just must use this line in your getView method:

Picasso.with(getContext()).load("http://myweb.com/images/"+(position+1)+".jpg").fit().into(imageView);

BTW, you can add picasso library in your gradle: compile 'com.squareup.picasso:picasso:2.5.2'

UPDATE: if you have your urls in a list, you must change the Picasso line like this:

Picasso.with(getContext()).load(listOfUrls.get(position)).fit().into(imageView);
Mohammad Zarei
  • 1,773
  • 14
  • 33
0
public String[] mThumbIds = {url1,url2,....,url10};

In getView method add this code

URL url = new URL(mThumbIds [position]);
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);

Edited:

Load image from url In the link see the answer given Kyle Clegg how to load image from url to imageview in a separate thread.

Community
  • 1
  • 1
Lingeshwaran
  • 579
  • 4
  • 15
  • `URL url = new URL(mThumbIds [position]);` give me a `unhandled exception java.net.malformedurlexception` AND `Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());` give me also : `unhandled exception java.io.ioexception` – MrMR May 02 '17 at 11:02