Android 使用线程加载图像formURL
如何在android中使用Android 使用线程加载图像formURL,android,Android,如何在android中使用线程从http加载图像? 我想在等待加载图像时显示布局 现在,我使用这段代码从http显示我的img,在显示结果之前,它会显示一个黑色的空白屏幕几次: private Drawable ImageOperations(Context ctx, String url, String saveFilename) { try { InputStream is = (InputStream) this.fetch(url)
线程从http加载图像?
我想在等待加载图像时显示布局
现在,我使用这段代码从http显示我的img,在显示结果之前,它会显示一个黑色的空白屏幕几次:
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) {
return null;
} catch (IOException e) {
return null;
}
}
public Object fetch(String address) throws MalformedURLException,IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
有人能帮我吗?您应该将ImageView对象传递给您的方法。方法作为线程运行。请解决您的问题。即使你的英语不是最好的,你也可以稍微修改一下你的语言(拼写错误、空格缺失、句子开头用大写字母、结尾用“.”等等)。那么
是什么意思?这是不是暗示你在问问题?我相信这没必要……谢谢你的建议,是的,我知道我的建议要少很多。不断学习
Use this for load images from http
ImageView img_item;
String image_url = "here url"
new Thread(new Runnable()
{
public void run()
{
try
{
final Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(image_url).getContent());
img_item.post(new Runnable()
{
public void run()
{
if(bitmap !=null)
{
img_item.setImageBitmap(bitmap);
}
}
});
} catch (Exception e)
{
// TODO: handle exception
}
}
}).start();