我是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
感谢您的光临。
答案 0 :(得分:1)
只需覆盖此方法......
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
collection = null;
}
答案 1 :(得分:0)
java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=6983KB, Allocated=3582KB, Bitmap Size=25132KB)
我会重新考虑你在做什么,因为基本上25MB的位图与你可以使用的字节数相比不会加载。