我前一天更新了应用程序,并且第一次收到用户发布的一些崩溃,如下所示:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:623)
at com.koushikdutta.urlimageviewhelper.UrlImageViewHelper.loadBitmapFromStream(UrlImageViewHelper.java:109)
at com.koushikdutta.urlimageviewhelper.UrlImageViewHelper.access$100(UrlImageViewHelper.java:27)
at com.koushikdutta.urlimageviewhelper.UrlImageViewHelper$1.onDownloadComplete(UrlImageViewHelper.java:582)
at com.koushikdutta.urlimageviewhelper.UrlImageViewHelper$3.doInBackground(UrlImageViewHelper.java:648)
at com.koushikdutta.urlimageviewhelper.UrlImageViewHelper$3.doInBackground(UrlImageViewHelper.java:645)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
... 3 more
我知道这与图像的处理有关。 (该应用程序从服务器检索大量文章,显示为图像和标题。)
我所做的一个快速解决方案是减小最大宽度为600像素的图像尺寸(图像大小可变)。
为此,我使用了com.koushikdutta.urlimageviewhelper
库,版本1.0.4。我不知道,是否还有其他库可以处理大量图像,以及如何使用它。
我在ListView
中使用的一种方式如下:
public class NewsAdapter extends BaseAdapter {
private ArrayList<NewsItem> data;
private Context context;
private LayoutInflater layoutInflater;
public NewsAdapter(Context context, ArrayList<NewsItem> data) {
this.data = data;
layoutInflater = LayoutInflater.from(context);
this.context = context;
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return data.get(position);
}
public long getItemId(int position) {
return position;
}
@SuppressLint({ "DefaultLocale", "InflateParams" })
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.news_row, null);
holder = new ViewHolder();
holder.image = (ImageView) convertView
.findViewById(R.id.image);
holder.name = (TextView) convertView
.findViewById(R.id.time);
holder.job = (TextView) convertView
.findViewById(R.id.title);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//change the font
Typeface typeFace=Typeface.createFromAsset(context.getAssets(),"fonts/Lato-Regular.ttf");
holder.name.setTypeface(typeFace);
holder.job.setTypeface(typeFace);
UrlImageViewHelper.setUrlDrawable(holder.image,
data.get(position).getImage_url());
holder.name.setText( data.get(position).getDate());
holder.job.setText( data.get(position).getTitle());
return convertView;
}
static class ViewHolder {
ImageView image;
TextView name, job;
}
}