I'm wondering how would I make an image that i开发者_开发知识库s located at a specific URL equal to an ImageView's image?
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();
}
// 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;
}
}
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.
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.
精彩评论