我正在使用毕加索来加载图像作为我活动的背景,我想使用AsyncTask,而图像正在加载,完成后,进度条会解散,以便为我的应用程序提供更好的外观,
这是我的代码:
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setMessage("Chargement...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
Picasso.with(MainActivity.this).load("http://tv2.orangeadd.com/mediacenter-data/ofc__bg_home.jpg").into(background,new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
mProgressDialog.dismiss();
}
@Override
public void onError() {
}
});
return null;
}
@Override
protected void onPostExecute(Void result) {
}
}
这总是显示错误并强制我的应用程序退出!
谢谢你们:)
答案 0 :(得分:13)
public void loadImageInBackground() {
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setMessage("Chargement...");
mProgressDialog.setIndeterminate(false);
Target target = new Target() {
@Override
public void onPrepareLoad(Drawable arg0) {
mProgressDialog.show();
}
@Override
public void onBitmapLoaded(Bitmap arg0, LoadedFrom arg1) {
background.setImageBitmap(arg0);
mProgressDialog.dismiss();
}
@Override
public void onBitmapFailed(Drawable arg0) {
// TODO Auto-generated method stub
mProgressDialog.dismiss();
}
};
Picasso.with(MainActivity.this)
.load("http://tv2.orangeadd.com/mediacenter-data/ofc__bg_home.jpg")
.into(target);
}
答案 1 :(得分:2)
我的猜测是错误是因为你试图修改后台线程中的UI元素(对话框),这是不可能的。
您不需要AsyncTask
,因为Picasso
已在后台进行解码。
答案 2 :(得分:2)
你得到错误,因为毕加索的加载功能已经是异步的。所以你可以在UI线程中这样做:
public void functionCalledFromUIThread(){
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setMessage("Chargement...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
Picasso.with(MainActivity.this).load("http://tv2.orangeadd.com/mediacenter-data/ofc__bg_home.jpg").into(background,new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
mProgressDialog.dismiss();
}
@Override
public void onError() {
mProgressDialog.dismiss();
}
});
}