38

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

Janusz
  • 182,484
  • 112
  • 300
  • 368
Skizit
  • 40,756
  • 89
  • 203
  • 267
  • 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,724
  • 1
  • 36
  • 55
Primal Pappachan
  • 24,967
  • 19
  • 66
  • 83
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,555
  • 18
  • 77
  • 110
ReachmeDroid
  • 949
  • 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,371
  • 2
  • 15
  • 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.