无法使用异步任务加载从URL图像 [英] Unable to load image from URL by using async task
本文介绍了无法使用异步任务加载从URL图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图从URL加载图像文本视图,但无法做到这一点,他们是没有错误,进度条也显示,但图像没有加载到图像视图,请大家帮忙,先谢谢了。
我的code ..
1。 XML:
< ImageView的
机器人:ID =@ + ID / ImageView的
机器人:layout_width =70dp
机器人:layout_height =70dp
机器人:layout_gravity =CENTER_HORIZONTAL
机器人:layout_marginTop =5DP
/>
2。 java的code ....
私有类Load_Product_Image扩展
AsyncTask的<无效,可绘制,绘制对象> { 私有对象fetch_image(字符串webaddress)
抛出MalformedURLException的,IOException异常{
网址URL =新的URL(webaddress);
对象内容= url.getContent();
返回的内容;
}在preExecute保护无效(){
super.on preExecute();
progressDialog =新ProgressDialog(getActivity(),ProgressDialog.THEME_TRADITIONAL);
progressDialog.setMessage(正在加载...);
progressDialog.setIndeterminate(假);
progressDialog.setCancelable(假);
progressDialog.show();
}@覆盖
保护可绘制doInBackground(虚空...... PARAMS){ InputStream为= NULL;
尝试{
为=(InputStream的)this.fetch_image(IMAGE_URL); //图像URI
// 地址
// 得到
//从
// previous
// toplist
//活动
//使用
//意图 }赶上(MalformedURLException的E){ e.printStackTrace();
}赶上(IOException异常五){ e.printStackTrace();
}
可绘制平局= Drawable.createFromStream(是,SRC);
回到平局;
} 保护无效onPostExecute(最终可绘制的结果){
尝试{
//显示图片...
runOnUiThread(新的Runnable(){
公共无效的run(){
profile_image.setImageDrawable(结果); }
});
progressDialog.dismiss();
}赶上(例外五){
// 没有
}
}
解决方案
毕加索 允许在您无忧图像加载应用程序经常在code的一行!
Picasso.with(上下文).load(http://i.imgur.com/DvpvklR.png).into(ImageView的);
Android上的图像加载的
许多常见的陷阱是由 毕加索 一个自动处理>
- 处理ImageView的回收和下载取消在适配器。
- 复杂的图像转换以最小的内存使用。
- 自动内存和磁盘高速缓存。
I am trying to load image from url to text view , but unable to do it, their is no error, progress bar is also appearing ,but image is not loading to image view , please help, thanks in advance
my code..
1. XML :
<ImageView
android:id="@+id/imageView"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
/>
2. java code....
private class Load_Product_Image extends
AsyncTask<Void, Drawable, Drawable> {
private Object fetch_image(String webaddress)
throws MalformedURLException, IOException {
URL url = new URL(webaddress);
Object content = url.getContent();
return content;
}
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(getActivity(),ProgressDialog.THEME_TRADITIONAL);
progressDialog.setMessage("Loading...");
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected Drawable doInBackground(Void... params) {
InputStream is = null;
try {
is = (InputStream) this.fetch_image(image_url);// image uri
// address
// getting
// from
// previous
// toplist
// activity
// using
// intent
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Drawable draw = Drawable.createFromStream(is, "src");
return draw;
}
protected void onPostExecute(final Drawable result) {
try {
// Display Image...
runOnUiThread(new Runnable() {
public void run() {
profile_image.setImageDrawable(result);
}
});
progressDialog.dismiss();
} catch (Exception e) {
// nothing
}
}
解决方案
Picasso allows for hassle-free image loading in your application—often in one line of code!
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Many common pitfalls of image loading on Android are handled automatically by Picasso
- Handling ImageView recycling and download cancelation in an adapter.
- Complex image transformations with minimal memory use.
- Automatic memory and disk caching.
这篇关于无法使用异步任务加载从URL图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文