这就是这段代码应该做的。
按下按钮时,它应该获取当前图像,并将其替换为字符串“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;
}