38

I'm wondering how would I make an image that is located at a specific URL equal to an ImageView's image?

Janusz
  • 187,060
  • 113
  • 301
  • 369
Skizit
  • 43,506
  • 91
  • 209
  • 269
  • 1
    Do you mean load an ImageView with an Image from a URL? Or upload an Image from an ImageView to a specific URL. – Robby Pond Jun 25 '10 at 14:48

4 Answers4

148

To download an image and set it as content for an imageview

try {
  ImageView i = (ImageView)findViewById(R.id.image);
  Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
  i.setImageBitmap(bitmap); 
} catch (MalformedURLException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}
COD3BOY
  • 11,964
  • 1
  • 38
  • 56
Primal Pappachan
  • 25,857
  • 22
  • 67
  • 84
7
  // Url = "url of image"
  Drawable drawable = LoadImageFromWebOperations(Url);
  mImageofTheMonth.setImageDrawable(drawable);

private Drawable LoadImageFromWebOperations(String url) {

    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
    } catch (Exception e) {
        System.out.println("Exc=" + e);
        return null;
    }

}
Mike Webb
  • 8,855
  • 18
  • 78
  • 111
ReachmeDroid
  • 959
  • 1
  • 14
  • 17
  • Do you think it is better to store the image locally on the device or just store the url to the image online instead? (assuming the image will always be there) – Micro Oct 14 '15 at 20:52
5

Since i don't have enough points to add a comment, I will make a post...

Remember to put @primpap's answer in AsyncTask doInBackground to prevent UI thread to freeze.

ymerdrengene
  • 1,421
  • 2
  • 17
  • 20
3
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ArrayList<ItemDetails> image_details = GetSearchResults();

        final ListView lv1 = (ListView) findViewById(R.id.listV_main);
        lv1.setAdapter(new ItemListBaseAdapter(this, image_details));

        lv1.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) { 

                // Launching new Activity on selecting single List Item
                Intent i = new Intent(getApplicationContext(), ItemDetails.class);

                Object o = lv1.getItemAtPosition(position);
                ItemDetails obj_itemDetails = (ItemDetails)o;

                // sending data to new activity
                i.putExtra("name", obj_itemDetails.getName());
                i.putExtra("description", obj_itemDetails.getItemDescription());
                i.putExtra("imagenumber", obj_itemDetails.getImageNumber());

                **ItemDetails.IMAGE_NUMBER = obj_itemDetails.getImageNumber();**
                startActivity(i);
            }   
        });
    }

Use Static Variable to get the Image ID and then Load it Dynamically. Check the IMAGE_Number.