Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java url中的图像我哪里出错了? 这就是这段代码应该做的。_Java_Android_Image_Url_Inputstream - Fatal编程技术网

Java url中的图像我哪里出错了? 这就是这段代码应该做的。

Java url中的图像我哪里出错了? 这就是这段代码应该做的。,java,android,image,url,inputstream,Java,Android,Image,Url,Inputstream,当按下按钮时,它应该获取当前图像,并将其替换为url中字符串“finalurl”中的图像 它做什么 它获取当前图像并使其显示。它不会替换为任何其他图像。我被这件事困扰了好几天,我很难受。感谢您的帮助 这是我使用的代码 public void firstbutton(View view) { Context context = view.getContext(); Drawable image = ImageOperations(context, "@st

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

它做什么 它获取当前图像并使其显示。它不会替换为任何其他图像。我被这件事困扰了好几天,我很难受。感谢您的帮助

这是我使用的代码

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;
}试试这个

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/finalur”,它试图从一些不存在的东西中提取一个图像。谢谢你的帮助!