您好,我如何在单独的线程中编写此代码,因为我的主线程中有密集操作。所以我必须使用异步活动并将网络密集操作委托给“doInBackground”方法。但我不知道如何编辑它
public void setImage(ImageView aView, final URL aURL) throws IOException {
final Bitmap bm = null;
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
URLConnection conn = null;
try {
conn = aURL.openConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn.connect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream is = null;
try {
is = conn.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Bufferisation pour le t�l�chargement
BufferedInputStream bis = new BufferedInputStream(is, 8192);
// Cr�ation de l'image depuis le flux des donn�es entrant
bm = BitmapFactory.decodeStream(bis);
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
};
task.execute(null, null, null);
// Fixe l'image sur le composant ImageView
aView.setImageBitmap(bm);
}
请帮帮我谢谢