我是 Android 新手,我正在尝试实现一个 ViewPager 来进行图像浏览。我正在使用一个名为 UrlImageViewHelper 的库从 URL 字符串中加载图像。滚动浏览前 4-5 张图像后,我遇到了内存泄漏问题。
这是我正在使用的页面适配器代码:
private class ImagePagerAdapter extends PagerAdapter {
private ArrayList<String> images = new ArrayList<String>();
private LayoutInflater inflater;
ImagePagerAdapter(ArrayList<String> imageUrls) {
this.images = imageUrls;
inflater = getLayoutInflater();
}
@Override
public void destroyItem(ViewGroup container, int position ,Object object) {
((ViewPager) container).removeView((View) object);
}
@Override
public int getCount() {
return images.size();
}
@Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = inflater.inflate(R.layout.activity_imagedetailitem, view, false);
ImageView imageView = (ImageView) imageLayout.findViewById(R.id.detailImage);
UrlImageViewHelper.setUrlDrawable(imageView, imageUrls.get(position));
((ViewPager) view).addView(imageLayout, 0);
return imageLayout;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
@Override
public void finishUpdate(View arg0) {}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {}
} //end of page adapter
非常基本的代码,但一定有我遗漏的东西吗?如果我能提供更多信息,请告诉我。谢谢大家。
UrlImageViewHelper 库中的代码:
/**
* Download and shrink an Image located at a specified URL, and display it
* in the provided {@link ImageView} once it finishes loading.
*
* @param imageView The {@link ImageView} to display the image to after it
* is loaded.
* @param url The URL of the image that should be loaded.
*/
public static void setUrlDrawable(final ImageView imageView, final String url) {
setUrlDrawable(imageView.getContext(), imageView, url, null, CACHE_DURATION_THREE_DAYS, null);
}
这是 LogCat 中抛出的异常:
04-02 09:53:08.975: E/AndroidRuntime(23001): FATAL EXCEPTION: AsyncTask #2
04-02 09:53:08.975: E/AndroidRuntime(23001): java.lang.RuntimeException: An error occured while executing doInBackground()
04-02 09:53:08.975: E/AndroidRuntime(23001): at android.os.AsyncTask$3.done(AsyncTask.java:200)
04-02 09:53:08.975: E/AndroidRuntime(23001): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
04-02 09:53:08.975: E/AndroidRuntime(23001): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
04-02 09:53:08.975: E/AndroidRuntime(23001): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
04-02 09:53:08.975: E/AndroidRuntime(23001): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
04-02 09:53:08.975: E/AndroidRuntime(23001): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
04-02 09:53:08.975: E/AndroidRuntime(23001): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
04-02 09:53:08.975: E/AndroidRuntime(23001): at java.lang.Thread.run(Thread.java:1027)
04-02 09:53:08.975: E/AndroidRuntime(23001): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=6983KB, Allocated=3582KB, Bitmap Size=25132KB)
04-02 09:53:08.975: E/AndroidRuntime(23001): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
04-02 09:53:08.975: E/AndroidRuntime(23001): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:694)
04-02 09:53:08.975: E/AndroidRuntime(23001): at com.koushikdutta.urlimageviewhelper.UrlImageViewHelper.loadBitmapFromStream(UrlImageViewHelper.java:117)
04-02 09:53:08.975: E/AndroidRuntime(23001): at com.koushikdutta.urlimageviewhelper.UrlImageViewHelper.access$3(UrlImageViewHelper.java:94)
04-02 09:53:08.975: E/AndroidRuntime(23001): at com.koushikdutta.urlimageviewhelper.UrlImageViewHelper$1.onDownloadComplete(UrlImageViewHelper.java:589)
04-02 09:53:08.975: E/AndroidRuntime(23001): at com.koushikdutta.urlimageviewhelper.UrlImageViewHelper$3.doInBackground(UrlImageViewHelper.java:655)
04-02 09:53:08.975: E/AndroidRuntime(23001): at com.koushikdutta.urlimageviewhelper.UrlImageViewHelper$3.doInBackground(UrlImageViewHelper.java:1)
04-02 09:53:08.975: E/AndroidRuntime(23001): at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-02 09:53:08.975: E/AndroidRuntime(23001): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
04-02 09:53:08.975: E/AndroidRuntime(23001): ... 4 more
感谢您的关注。