Android,在与ImageView的图像相同的URL上制作图像
我想知道如何使图像位于特定的URL等于ImageView的图像?
下载图像并将其设置为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; } }
由于我没有足够的分数来添加评论,所以我会发一个post…
记住把@ primpap的答案在AsyncTask doInBackground中,以防止UI线程冻结。
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); } }); }
使用静态variables获取图像ID,然后dynamic加载它。 检查IMAGE_Number。