没有错误的处理程序但不显示图像 [英] not error on handler but not display image
本文介绍了没有错误的处理程序但不显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我没有得到错误,但不能显示在活动图像
i not have get error but can't display image on activity
MainActivity:
MainActivity :
Handler handler = new Handler();
EditText inputUrl;
OnClickListener getImageBtnOnClick = new OnClickListener() {
public void onClick(View view) {
Context context = view.getContext();
Editable ed = inputUrl.getText();
Drawable image = ImageOperations(context,ed.toString(),"image.jpg");
final ImageView imgView = new ImageView(context);
imgView.setImageDrawable(image);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
imgView.setImageResource(R.id.imageView1);
}
}, 10000);
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputUrl = ((EditText)findViewById(R.id.editText1));
inputUrl.setSingleLine();
inputUrl.setTextSize(11);
Button getImageButton = (Button)findViewById(R.id.button1);
getImageButton.setOnClickListener(getImageBtnOnClick);
}
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;
}
如何解决呢?区间显示和运行的形象?感谢的
how to fix it ? display and run image with interval ? thank's
推荐答案
删除
imgView.setImageResource(R.id.imageView1);
和使用
image.setImageDrawable(image); // image is your drawable
在您的处理程序,使之成为
in your handler so that it becomes,
handler.postDelayed(new Runnable() {
public void run() {
image.setImageDrawable(image);
}
}, 10000);
这篇关于没有错误的处理程序但不显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文