【问题标题】:Image from url-Where did i go wrong?图片来自 url-我哪里出错了?
【发布时间】:2012-07-12 20:34:40
【问题描述】:

这就是这段代码应该做的。

当按钮被按下时,它应该获取当前图像,并将其替换为字符串“finalurl”中的 url 中的图像

它的作用

它获取当前图像并使其消失。它不会替换为任何其他图像。我已经坚持了几天,我很难过。任何帮助表示赞赏。

这是我使用的代码。

public void firstbutton(View view) 
    {
        Context context = view.getContext();
        Drawable image = ImageOperations(context, "@string/finalurl","image.jpg");
        ImageView icon = new ImageView(context);
        icon = (ImageView)findViewById(R.id.imageView);
        icon.setImageDrawable(image);

};


private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
    try {
        InputStream is = (InputStream) this.fetch(url);
        Drawable d = Drawable.createFromStream(is, "src");
        return d;
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

public Object fetch(String address) throws MalformedURLException,IOException {
    URL url = new URL(address);
    Object content = url.getContent();
    return content;

}

【问题讨论】:

标签: java android image url inputstream


【解决方案1】:

试试这个。

Drawable drawable = LoadImageFromWebOperations(ImageLink);
        imageView.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;
      }
    }

【讨论】:

  • 我明白我做错了什么。我提供给 ImageOperations 方法的 url 不是 url。我实际上给了它“@string/finalurl”,它试图从不存在的东西中提取图像。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2016-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-14
  • 2013-12-25
  • 2020-12-09
  • 1970-01-01
相关资源
最近更新 更多