Android 可抽出式韩元';不能使用异步任务加载
我有一个queryAppIcon()方法,可以在数组appIconDrawable中查询和存储图像。然而,我只是在应该弹出图像的地方变得空白。请让我知道我是否应该发布任何其他相关代码 这是ViewActivity中的相关代码:Android 可抽出式韩元';不能使用异步任务加载,android,android-asynctask,Android,Android Asynctask,我有一个queryAppIcon()方法,可以在数组appIconDrawable中查询和存储图像。然而,我只是在应该弹出图像的地方变得空白。请让我知道我是否应该发布任何其他相关代码 这是ViewActivity中的相关代码: // global vars final Drawable[] appIconDrawable = null; int i; public Drawable[] queryAppIcon() throws ParseException, IOException {
// global vars
final Drawable[] appIconDrawable = null;
int i;
public Drawable[] queryAppIcon() throws ParseException, IOException {
ParseQuery<ParseObject> query = ParseQuery.getQuery("AndroidStoreContent");
query.whereExists("appIcon");
List<ParseObject> ParseResult = query.find();
// Drawable array
appIconDrawable = new Drawable[ParseResult.size()];
for (i = 0; i < ParseResult.size(); i++) {
ParseFile pf = (ParseFile) ParseResult.get(i).get("appIcon");
startDownload(pf);
}
return appIconDrawable;
}
public void startDownload(ParseFile pf) {
new DownloadImageTask(this).execute(pf);
}
public class DownloadImageTask extends AsyncTask<ParseFile, Void, Drawable> {
private AsyncResponse ar;
DownloadImageTask(AsyncResponse ar) {
this.ar = ar;
}
@Override
protected Drawable doInBackground(ParseFile... pf) {
return fetchDrawable(pf[0]);
}
protected void onPostExecute(Drawable result) {
ar.processFinish(result);
}
public Drawable fetchDrawable(ParseFile pf) {
InputStream is;
try {
is = (InputStream) new URL(pf.getUrl()).getContent();
return Drawable.createFromStream(is,null);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
@Override
public void processFinish(Drawable d) {
appIconDrawable[i] = d; // i also tried testing appIconDrawable[1] = d and the app loaded with all blank images and then crashes
}
看起来你需要一点重构 您希望从
queryAppIcon()
下载Drawable[]
,但您将始终得到一个空集,因为您在单独的线程中启动下载,然后更新返回值
您应该从processFinish
中将Drawable
设置为ImageView
出错的迹象:异步方法(如下载图像)永远不会有返回值
更新
这是一个非常简单的异步下载任务,但是缺少很多检查、优化等,比如缓存!另外,下载ImageTask中的ImageView应该由WeakReference
(谷歌it)持有,否则它会泄露你的活动
public class DownloadImageTask extends AsyncTask<String, Void, Drawable> {
private static final String TAG = DownloadImageTask.class.getSimpleName();
private ImageView mImageView;
DownloadImageTask(ImageView imageView) {
mImageView = imageView;
}
@Override
protected Drawable doInBackground(String... url) {
return fetchDrawable(url[0]);
}
@Override
protected void onPostExecute(Drawable result) {
if (result != null) {
mImageView.setImageDrawable(result);
} else {
Log.w(TAG, "Could download image!");
}
}
public static Drawable fetchDrawable(String url) {
Log.v(TAG, "Downloading: " + url);
InputStream is;
try {
is = (InputStream) new URL(url).getContent();
return Drawable.createFromStream(is, null);
} catch (MalformedURLException e) {
Log.e(TAG, e.getMessage(), e);
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
}
return null;
}
}
谢谢我试图这样做,但是ImageView是在
ViewAdapter
类中设置的,而AsyncTask是在ViewActivity
类中设置的。有办法解决这个问题吗?我应该发布我的适配器和活动类吗?你反对使用库吗?看:是的,我见过,但我不想用图书馆来做这个project@ErmiasAsghedom添加了一个示例类,但将其作为一个概念,而不是复制和粘贴代码。
public class DownloadImageTask extends AsyncTask<String, Void, Drawable> {
private static final String TAG = DownloadImageTask.class.getSimpleName();
private ImageView mImageView;
DownloadImageTask(ImageView imageView) {
mImageView = imageView;
}
@Override
protected Drawable doInBackground(String... url) {
return fetchDrawable(url[0]);
}
@Override
protected void onPostExecute(Drawable result) {
if (result != null) {
mImageView.setImageDrawable(result);
} else {
Log.w(TAG, "Could download image!");
}
}
public static Drawable fetchDrawable(String url) {
Log.v(TAG, "Downloading: " + url);
InputStream is;
try {
is = (InputStream) new URL(url).getContent();
return Drawable.createFromStream(is, null);
} catch (MalformedURLException e) {
Log.e(TAG, e.getMessage(), e);
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
}
return null;
}
}
public class ImageDownloadAdapter extends ArrayAdapter<String>{
public ImageDownloadAdapter(Context context, String[] objects) {
super(context, R.layout.item_image_download, R.id.txt_url, objects);
}
@SuppressLint("NewApi")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
String url = getItem(position);
ImageView imageView = (ImageView) view.findViewById(R.id.img_download);
DownloadImageTask downloadImageTask = new DownloadImageTask(imageView);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
downloadImageTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url);
} else {
downloadImageTask.execute(url);
}
return view;
}
}
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(new ImageDownloadAdapter(this, new String[]{
"http://www.beautystat.com/site/wp-content/uploads/2011/02/happy-faces-small.jpg",
"http://www.ducthide.com/new_wallet_pics/happy_face.JPG"
}));