Android,在与 ImageView 图像相同的 URL 处制作图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3118691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:48:03  来源:igfitidea点击:

Android, Make an image at a URL equal to ImageView's image

android

提问by Skizit

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

我想知道如何使位于特定 URL 的图像等于 ImageView 的图像?

回答by Primal Pappachan

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();
}

回答by ReachmeDroid

  // 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;
    }

}

回答by ymerdrengene

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

由于我没有足够的积分来添加评论,我会发一个帖子......

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

请记住将@primpap 的答案放在AsyncTask doInBackground 中以防止 UI 线程冻结。

回答by ishtiyaqm15

 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.

使用静态变量获取图像 ID,然后动态加载它。检查 IMAGE_Number。