0

我是Android新手,我試圖實現ViewPager進行圖片瀏覽。我使用名爲UrlImageViewHelper的庫來加載來自URL字符串的圖像。滾動瀏覽第4-5張圖片後,我遇到內存泄漏問題。使用圖片的「內存不足」錯誤ViewPager

這裏是頁適配器代碼我使用:

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

後的到底是什麼,你做的異常和UrlImageViewHelper.setUrlDrawable的'代碼()'。 – hardartcore

回答

0
java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=6983KB, Allocated=3582KB, Bitmap Size=25132KB) 

我會重新考慮你做的事情基本上25MB位圖是不是要去負載與您可以使用的字節數。

+0

25MB的位圖大小是否意味着位圖不能正確釋放? –

+0

沒有看到您嘗試加載的文件目前我還不知道,但是如果您深入瞭解右側列出的帖子,進入「相關」列,那麼我認爲這將是一個好的開始。 –

1

只需重寫此方法...

@Override 
public void destroyItem(View collection, int position, Object view) { 
    ((ViewPager) collection).removeView((View) view); 
    collection = null; 
}