2016-06-08 72 views
2

我想將圖像加載到ImageView中,並且正在接收OutOfMemoryError。圖像根據ViewPager中的特定視圖動態顯示。但是,在我通過ViewPager滾動後,圖像最終無法加載並導致OutOfMemoryError。使用Picasso Image的Java OutOfMemoryError

請注意,我在ViewPager 4視圖,應用程序出現崩潰的視圖2.我猜的形象簡直是大大小,但我的印象是,畢加索相應的處理:

畢加索代碼:

Picasso.with(getActivity()) 
    .load(mImageURL) 
    .fit() 
    .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_CACHE) 
    .into((ImageView) rootView.findViewById(R.id.comments_image)); 

XML

<ImageView 
    android:id="@+id/comments_image" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight=".525" 
    android:background="@color/black"> 
</ImageView> 

錯誤

java.lang.OutOfMemoryError: Failed to allocate a 5074572 byte allocation with 132096 free bytes and 129KB until OOM 
    at dalvik.system.VMRuntime.newNonMovableArray(Native Method) 
    at android.graphics.Bitmap.nativeCreate(Native Method) 
    at android.graphics.Bitmap.createBitmap(Bitmap.java:812) 
    at android.graphics.Bitmap.createBitmap(Bitmap.java:789) 
    at android.graphics.Bitmap.createBitmap(Bitmap.java:756) 
    at android.graphics.drawable.VectorDrawable$VectorDrawableState.createCachedBitmapIfNeeded(VectorDrawable.java:713) 
    at android.graphics.drawable.VectorDrawable.draw(VectorDrawable.java:280) 
    at com.squareup.picasso.PicassoDrawable.draw(PicassoDrawable.java:105) 
    at android.widget.ImageView.onDraw(ImageView.java:1176) 
    at android.view.View.draw(View.java:15231) 
    at android.view.View.updateDisplayListIfDirty(View.java:14167) 
    at android.view.View.getDisplayList(View.java:14189) 
    at android.view.View.draw(View.java:14959) 
    at android.view.ViewGroup.drawChild(ViewGroup.java:3405) 
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198) 
    at android.view.View.draw(View.java:15234) 
    at android.view.View.updateDisplayListIfDirty(View.java:14167) 
    at android.view.View.getDisplayList(View.java:14189) 
    at android.view.View.draw(View.java:14959) 
    at android.view.ViewGroup.drawChild(ViewGroup.java:3405) 
    at android.support.design.widget.CoordinatorLayout.drawChild(CoordinatorLayout.java:1131) 
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198) 
    at android.view.View.updateDisplayListIfDirty(View.java:14162) 
    at android.view.View.getDisplayList(View.java:14189) 
    at android.view.View.draw(View.java:14959) 
    at android.view.ViewGroup.drawChild(ViewGroup.java:3405) 
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198) 
    at android.view.View.draw(View.java:15234) 
    at android.support.v4.view.ViewPager.draw(ViewPager.java:2341) 
    at android.view.View.updateDisplayListIfDirty(View.java:14167) 
    at android.view.View.getDisplayList(View.java:14189) 
    at android.view.View.draw(View.java:14959) 
    at android.view.ViewGroup.drawChild(ViewGroup.java:3405) 
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198) 
    at android.view.View.updateDisplayListIfDirty(View.java:14162) 
    at android.view.View.getDisplayList(View.java:14189) 
    at android.view.View.draw(View.java:14959) 
    at android.view.ViewGroup.drawChild(ViewGroup.java:3405) 
    at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1373) 
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198) 
    at android.view.View.updateDisplayListIfDirty(View.java:14162) 
    at android.view.View.getDisplayList(View.java:14189) 
    at android.view.View.draw(View.java:14959) 
    at android.view.ViewGroup.drawChild(ViewGroup.java:3405) 
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198) 
    at android.view.View.updateDisplayListIfDirty(View.java:14162) 
    at android.view.View.getDisplayList(View.java:14189) 
    at android.view.View.draw(View.java:14959) 
    at android.view.ViewGroup.drawChild(ViewGroup.java:3405) 
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198) 
    at android.view.View.updateDisplayListIfDirty(View.java:14162) 
    at android.view.View.getDisplayList(View.java:14189) 
    at android.view.View.draw(View.java:14959) 
    at android.view.ViewGroup.drawChild(ViewGroup.java:3405) 
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198) 
    at android.view.View.updateDisplayListIfDirty(View.java:14162) 
    at android.view.View.getDisplayList(View.java:14189) 
    at android.view.View.draw(View.java:14959) 
    at android.view.ViewGroup.drawChild(ViewGroup.java:3405) 
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198) 
    at android.view.View.updateDisplayListIfDirty(View.java:14162) 
    at android.view.View.getDisplayList(View.java:14189) 
    at android.view.View.draw(View.java:14959) 
    at android.view.ViewGroup.drawChild(ViewGroup.java:3405) 
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198) 
    at android.view.View.draw(View.java:15234) 
    at android.widget.FrameLayout.draw(F 
+0

什麼是圖像的分辨率? – Vucko

+0

我知道這是359.2 KB,它只是太大,但我希望瞭解畢加索如何壓縮以防止這種錯誤。圖片位於:https://s3.amazonaws.com/fan-polls/Durant.jpg – tccpg288

+0

正如我前一段時間所理解的,它更多地與它對尺寸所做的分辨率有關。我對畢加索並不熟悉,我聽說過。希望有人能更好地幫助你。 – Vucko

回答

1

你在你最有型的文件中製作了android:largeheap =「true」嗎?

<application 
    android:name=".MyApplication" 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="MyApplication" 
    android:largeHeap="true"* 
    android:logo="@drawable/logo_for_up" 
    android:screenOrientation="portrait" 
    android:theme="@style/AppTheme" > 
</application> 
+0

這實際上是做什麼的,在我實現它之前,你能幫助我理解嗎? – tccpg288

+0

運行在API Level 11+上的應用程序可以在清單中的元素上使用android:largeHeap =「true」來請求大於正常的堆大小,並且ActivityManager上的getLargeMemoryClass()會告訴您堆有多大。 –

+0

但是,並不能保證大堆的大小。 用戶會感覺到你的大堆請求,因爲它會迫使他們的其他應用程序離開內存,終止其他應用程序的進程以釋放系統RAM以供大堆使用。 –

2

我建議你使用Glide,因爲它被設計爲在低成本(和最差的硬件)設備上默認工作。

畢加索和Glide之間的巨大差異在於,當畢加索的ARGB_8888時,Glide的默認格式是RGB_565。正如您在下面看到的,它的成本比畢加索低50%,可以做同樣的工作。

Memory cost

不過,如果你沒有達到改變你的圖書館,你可以嘗試使用.resize(int, int)方法對畢加索的建設者調整你的形象。

+0

我注意到該錯誤發生在三星模擬器上,但我也運行的HTC模擬器沒有問題。 – tccpg288

+0

我有興趣設置100Kb的最大尺寸,關於如何去做這件事的任何想法? – tccpg288

+0

我不認爲有一種方法可以用畢加索來達到這個目的,因爲它必須計算哪種尺寸(寬度和高度的組合)會在你的記憶中產生最大值100Kb。 –