Downloading Image in android using asynctask for RecyclerView(使用 asynctask 为 RecyclerView 在 android 中下载图像)
问题描述
这是我的以下 AsyncTask
类代码,用于下载 RecyclerView
的图像.
This my following AsyncTask
class code for downloading image for RecyclerView
.
public class MyDownloadImageAsyncTask extends AsyncTask<String, Void,Bitmap> {
private final WeakReference<ImageView> imageViewReference;
public MyDownloadImageAsyncTask(ImageView imv) {
imageViewReference = new WeakReference<ImageView>(imv);
}
@Override
protected Bitmap doInBackground(String... urls) {
Bitmap bitmap = null;
for (String url : urls) {
bitmap = MyUtility.downloadImage(url);
/*if (bitmap != null) {
mImgMemoryCache.put(url, bitmap);
}*/
}
return bitmap;
}
protected void onPostExecute(Bitmap bitmap){
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
}
我以这种方式调用 Adapter
中的 AsyncTask
:
I call the AsyncTask
in my Adapter
this way:
MyDownloadImageAsyncTask task = new MyDownloadImageAsyncTask(holder.vIcon);
task.execute(new String[] {(String)movie.get("image")}););
每次运行该应用程序时都会崩溃.用于下载图像的 URL 位于 ArrayList
中.
The app is crashing every time I run it. The URL for downloading the image is in a ArrayList
.
我猜我在调用 AsyncTask
时犯了这个错误,但我想不出解决办法.
I guess the mistake I'm doing this in calling the AsyncTask
but I couldn't figure out the solution.
推荐答案
改变这个
public MyDownloadImageAsyncTask(ImageView imv) {
imageViewReference = new WeakReference(imv);
}
到这里
public MyDownloadImageAsyncTask(ImageView imv) {
imageViewReference = new WeakReference<ImageView>(imv);
}
这是我使用的代码,它运行完美
Here is the code that i use and it works perfect
class LoadImage extends AsyncTask<String, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
public LoadImage(ImageView imageView) {
imageViewReference = new WeakReference<ImageView>(imageView);
}
@Override
protected Bitmap doInBackground(String... params) {
try {
return downloadBitmap(params[0]);
} catch (Exception e) {
// log error
}
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
}
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
} else {
Drawable placeholder = imageView.getContext().getResources().getDrawable(R.drawable.ic_launcher);
imageView.setImageDrawable(placeholder);
}
}
}
}
private Bitmap downloadBitmap(String url) {
HttpURLConnection urlConnection = null;
try {
URL uri = new URL(url);
urlConnection = (HttpURLConnection) uri.openConnection();
int statusCode = urlConnection.getResponseCode();
if (statusCode != HttpStatus.SC_OK) {
return null;
}
InputStream inputStream = urlConnection.getInputStream();
if (inputStream != null) {
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
} catch (Exception e) {
urlConnection.disconnect();
Log.w("ImageDownloader", "Error downloading image from " + url);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return null;
}
}
这是我从我的 ADAPTER
new LoadImage(holder.itemImage).execute(IMAGE_URL);
单独用于每个 URL.
seperately for every URL.
如果它对你有帮助,试试这个.
Try this if it helps you out.
这篇关于使用 asynctask 为 RecyclerView 在 android 中下载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 asynctask 为 RecyclerView 在 android 中下载图像
基础教程推荐
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 降序排序:Java Map 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01