【问题标题】:My app is displaying the image url. How do i download the image so that it can display on my app我的应用正在显示图片网址。我如何下载图像以便它可以显示在我的应用程序上
【发布时间】:2017-09-17 09:23:33
【问题描述】:

//我的适配器。 我正在尝试找到一种方法来在下面的代码中实现毕加索或它的活动来显示图像。我还使用 gridview 来显示图像。下面的getImage()是负责获取url的方法

        public CategoryViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            mContext = itemView.getContext();
        }

        public void bindCategory(Category category) {
            mNameTextView.setText(category.getImage());
        }
    }
}

【问题讨论】:

  • 你想在调用 getImage() 时显示图像吗?
  • 是的@LokkeshwaranJ

标签: java android api flickr


【解决方案1】:

您可以从 url 中获取 InputStream,并解码位图以将其显示在 ImageView 中。

try {
    URL url = new URL(getImage());
    Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
    imageView.setImageBitmap(bitmap);
} catch (IOException e) {
    e.printStackTrace();
}

我建议你使用 Picasso 库,使用它你可以使用以下代码轻松加载图像:

Picasso.with(this/*context*/).load(getImage()).into(imageView);

编辑:

因为您希望直接从该方法中获取图像,所以如下:

public Bitmap getImage(){
    String imageUrl  = "https://farm"+getFarm()+".staticflickr.com/"+getServer()
        +"/"+getId()+"_"+getSecret()+"_m.jpg";
    try {
        URL url = new URL(imageUrl);
        return BitmapFactory.decodeStream(url.openStream());
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

【讨论】:

  • 有没有办法在调用 getImage() 时做到这一点
  • @grace 检查编辑后的答案。使用 AsyncTask 中的函数,因为此函数会阻塞 UI 线程。
  • 我尝试使用它,但它没有用。我有一个数组适配器,我无法将 imageView 设置为数组适配器
【解决方案2】:

您可以在 Web 视图中打开图像。确保您的 Activity Xml 中有一个 WebView 组件,并在您的类(全局变量)之外声明 WebView 变量,以便您可以在函数中使用它。

public void getImage(){
        String imageUrl  = "https://farm"+getFarm()+".staticflickr.com/"+getServer()+"/"+getId()+"_"+getSecret()+"_m.jpg";
        mWebView.loadDataWithBaseURL(null, "<html><head></head><body><table style=\"width:100%; height:100%;\"><tr><td style=\"vertical-align:middle;\"><img src=\"" + imageUrl + "\"></td></tr></table></body></html>", "html/css", "utf-8", null);

    }

【讨论】:

    【解决方案3】:

    您可以使用自定义库查看这样的图像

    Picasso.with(this).load(getImage()).into(imageView);
    

    Glide.with(this).load(getImage()).into(imageView);
    

    并下载图片:

    public void DownloadImageFromPath(String path){
                InputStream in =null;
                Bitmap bmp=null;
                 ImageView iv = (ImageView)findViewById(R.id.img1);
                 int responseCode = -1;
                try{
    
                     URL url = new URL(path);//"http://192.xx.xx.xx/mypath/img1.jpg
                     HttpURLConnection con = (HttpURLConnection)url.openConnection();
                     con.setDoInput(true);
                     con.connect();
                     responseCode = con.getResponseCode();
                     if(responseCode == HttpURLConnection.HTTP_OK)
                     {
                         //download 
                         in = con.getInputStream();
                         bmp = BitmapFactory.decodeStream(in);
                         in.close();
                         iv.setImageBitmap(bmp);
                     }
    
                }
                catch(Exception ex){
                    Log.e("Exception",ex.toString());
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2020-06-07
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多