2014-05-18 85 views
0

我運行這個程序它是正常顯示,但當我點擊這個圖像或滾動這個圖像它崩潰了。請幫助我!Android上的圖像畫廊3

我的代碼在這裏:enter link description here

我的錯誤:

05-18 14:18:56.064: E/AndroidRuntime(1375): FATAL EXCEPTION: main 
05-18 14:18:56.064: E/AndroidRuntime(1375): java.lang.OutOfMemoryError 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.content.res.Resources.loadDrawable(Resources.java:1965) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.content.res.Resources.getDrawable(Resources.java:660) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.widget.ImageView.resolveUri(ImageView.java:616) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.widget.ImageView.setImageResource(ImageView.java:349) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at com.example.gallerycookbook.GalleryImageAdapter.getView(GalleryImageAdapter.java:47) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.widget.Gallery.makeAndAddView(Gallery.java:865) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.widget.Gallery.fillToGalleryRightLtr(Gallery.java:819) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.widget.Gallery.fillToGalleryRight(Gallery.java:763) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.widget.Gallery.trackMotionScroll(Gallery.java:393) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.widget.Gallery.onScroll(Gallery.java:1043) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.view.GestureDetector.onTouchEvent(GestureDetector.java:572) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.widget.Gallery.onTouchEvent(Gallery.java:958) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.view.View.dispatchTouchEvent(View.java:7246) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.app.Activity.dispatchTouchEvent(Activity.java:2410) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.view.View.dispatchPointerEvent(View.java:7426) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271) 
05-18 14:18:56.064: E/AndroidRuntime(1375):  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363) 

回答

1

此行顯示了問題:

05-18 14:18:56.064: E/AndroidRuntime(1375): FATAL EXCEPTION: main 
05-18 14:18:56.064: E/AndroidRuntime(1375): java.lang.OutOfMemoryError 

從Oracle文檔:

當Java虛擬機無法分配一個對象時拋出,因爲它是內存不足,並且可以提供沒有更多的內存由垃圾收集器。 OutOfMemoryError對象可以由虛擬機構造,就像抑制被禁用和/或堆棧跟蹤不可寫一樣。

使用此代碼來按比例縮小的圖片:

private Bitmap downscaleBitmapUsingDensities(final int sampleSize,final int imageResId) 
    { 
    final Options bitmapOptions=new Options(); 
    bitmapOptions.inDensity=sampleSize; 
    bitmapOptions.inTargetDensity=1; 
    final Bitmap scaledBitmap=BitmapFactory.decodeResource(getResources(),imageResId,bitmapOptions); 
    scaledBitmap.setDensity(Bitmap.DENSITY_NONE); 
    return scaledBitmap; 
    } 
+0

感謝您@Andrey馬爾可夫 – user3623959

+0

您的歡迎:) –

0

你跑出來的內存。通常,當您嘗試在一個視圖中顯示多個圖像時。您應該高效地使用位圖。一種方法是使用類似通用圖像加載器的庫,它使用lazyloading。有關更多詳細信息,請參閱here

0

您正在嘗試打開填充android給予處理的整個內存的圖像。爲了避免它,你必須在展示之前下載圖片。我建議使用非常酷的庫 - Android Query