0

In my program i am using local images, but now i want to use online images instead of Drawable

But i don't know what are the changes i have to implement in my code, to get my target done for me.

here is my code, which i am using for drawable images, see:

public int[] IMAGE_IDS = { 
        R.drawable.flower_01, R.drawable.flower_02,
        R.drawable.flower_01, R.drawable.flower_02,
        R.drawable.flower_01, R.drawable.flower_02,
        R.drawable.flower_01, R.drawable.flower_02
        };

And Adapter class, complete code looks like this:

public class AddImageAdapter extends BaseAdapter
{   
    Context mycontext = null;
    int galitembg = 0;

    public int[] IMAGE_IDS = { 
        R.drawable.flower_01, R.drawable.flower_02,
        R.drawable.flower_01, R.drawable.flower_02,
        R.drawable.flower_01, R.drawable.flower_02,
        R.drawable.flower_01, R.drawable.flower_02
        };

    public AddImageAdapter(Context c) 
    {
        mycontext = c;
        TypedArray typArray = mycontext.obtainStyledAttributes(R.styleable.GalleryTheme);
        galitembg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
        typArray.recycle();
    }

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

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

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

     @Override
     public View getView(int position, View convertView, ViewGroup parent) 
     {
         ImageView imageview = new ImageView(mycontext);
         imageview.setImageResource(IMAGE_IDS[position]);
         imageview.setScaleType(ImageView.ScaleType.FIT_XY);
         return imageview;
     }

}

I wanna use these two live images URLs in place of drawable

URL-1: http://files.vividscreen.com/soft/4f9891a78a355e9c4d15fc469eb98625/Sunflowers-480x800.jpg

URL-2: http://www.hdiphonewallpapers.us/phone-wallpapers/freewallpaper/12960425OV940-3Q61.jpg

Sun
  • 6,768
  • 25
  • 76
  • 131
  • try universal image loader library https://github.com/nostra13/Android-Universal-Image-Loader – Pr38y Aug 21 '14 at 09:54
  • follow this : http://stackoverflow.com/questions/25077177/outofmemoryexception-load-bunch-of-images-from-server – Pragnesh Ghoda シ Aug 21 '14 at 09:55
  • don't wanna use any library .... – Sun Aug 21 '14 at 09:55
  • @Prag'sシ still i am just making a play ball code for learning purpose but at the end i will have to fetch images from JSON, and i am also showing dots at bottom – Sun Aug 21 '14 at 10:06

2 Answers2

2
   new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
                .execute("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");
    }

    public void onClick(View v) {
        startActivity(new Intent(this, IndexActivity.class));
        finish();

    }

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

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

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            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);
        }
    }

You can download images from url using this method and set it in imageview. you just need the internet permission to download the images

onexf
  • 3,674
  • 3
  • 22
  • 36
Google
  • 2,183
  • 3
  • 27
  • 48
1

You can use this library Picasso. This library download images from a url and disk cached also many more options.

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

How to use:

joselufo
  • 3,393
  • 3
  • 23
  • 37