gpt4 book ai didi

php - 如何在异步任务上加载图像

转载 作者:行者123 更新时间:2023-11-29 12:46:56 24 4
gpt4 key购买 nike

Android (Java) 使用 asynctask 加载图像

        @Override
protected String doInBackground(String... arg0) {
mNewsList = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(URL);
try {
mNews = json.getJSONArray(TAG_POSTS);
for (int i = 0; i < mNews.length(); i++) {
JSONObject c = mNews.getJSONObject(i);
String image = c.getString(TAG_IMAGE);

HashMap<String, String> map = new HashMap<String, String>();

map.put(TAG_IMAGE, image);
mNewsList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}

protected void onPostExecute(String file_url) {
ListAdapter adapter = new SimpleAdapter(Home.this, mNewsList,
R.layout.news_list, new String[] {TAG_IMAGE},
new int[]{R.id.ivImage});
newsList.setAdapter(adapter);
}

如何使用asynctask加载图像

尝试使用惰性列表适配器,但不适合我的需求。

最佳答案

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;

public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}

protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}

protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}

}

关于php - 如何在异步任务上加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25302743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com