【发布时间】:2016-04-01 22:19:09
【问题描述】:
我无法理解 AsyncTask。我正在向它发送一个文件位置,以便它可以检索它、调整它的大小并返回它。当我从我的活动(BaseAdapter ListView)中调用 asynctask 类时,我遇到了类型不匹配。
我认为我可以从执行中返回位图。我读过我需要使用 onPostExecute() 但不确定如何..
public class ImageHandler extends AsyncTask<String, Void, Bitmap> {
Bitmap sizedBMP = null;
@Override
protected Bitmap doInBackground(String... params) {
File imgFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath(), "/"+params[0]+"/"+params[0]+".png");
if (imgFile.exists()){
Bitmap bmp = BitmapFactory.decodeFile(imgFile.toString());
int newWidth = 500;
sizedBMP = getResizedBitmap(bmp, newWidth);
}
else{
//set no image available
}
return sizedBMP;
}
@Override
// Once the image is downloaded, associates it to the imageView
protected void onPostExecute(Bitmap sizedBMP) {
}
public Bitmap getResizedBitmap(Bitmap bm, int newWidth) {
// Bitmap gets resized here.....
}
}
我在 getView() 中调用这个类,例如:
ImageHandler imgHandler = new ImageHandler();
Bitmap bitMap;
bitMap = imgHandler.execute(filename);
错误:
类型不匹配:无法从 AsyncTask 转换为 位图
【问题讨论】:
标签: android bitmap android-asynctask baseadapter