trip07
trip07

Reputation: 187

outofmemoryerror when scrolling scrollview with imageview

Please I have troubble with my app.

My problem is with outofmemoryerror error.

I try explain my method

Thise error arises when scrolling scrollview In which it is imageview, size of imageview matters of items (it's image with lines of text items 0-x) when have 23< items and scrolling app greatly increases memory usages. But I don't know how prevent it?

I paste part of my class which insert bitmap(canvas) into imageview.

try {
        Bitmap b = Bitmap.createBitmap(srk, vsk, Bitmap.Config.ARGB_8888);

        c = new Canvas(b);
        imageview.setImageBitmap(b);

    } catch (IllegalArgumentException e) {
    }

    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.FILL);


    for (int i=1;i<(prvy.getNasledujuciitemindex(pacage));i++) {
        if (prvy.citajItem(pacage,i)!=null) {

            if (prvy.CiZbalene(pacage, i)) {
                paint.setColor(Color.argb(255, 130, 130, 130));
                c.drawRect(zarovnanieXZACDelLine, zarovnanieYDelLine + (i - 1) * vskconst, zarovnanieXKONDelLine, zarovnanieYDelLine + 2 + (i - 1) * vskconst, paint);

                c.drawBitmap(cross, zarovnanieXX, (float) (context.getResources().getDimensionPixelSize(R.dimen.KorekciaLine) + (int) (i - 1) * vskconst), paint);
                paint.setColor(Color.argb(255, 130, 130, 130));
                DalsiPrvok(paint, c, prvy.citajItem(pacage, i), vsk2 + (i - 1) * vskconst, sizetext, font);
            } else {
                paint.setColor(Color.BLACK);
                DalsiPrvok(paint, c, prvy.citajItem(pacage, i), vsk2 + (i - 1) * vskconst, sizetext, font);
            }
        } else {break;}

    }
    imageview.invalidate();

only this code write into imageview.

here are my xml when have imageview.

            <ScrollView
                        android:id="@+id/scrollView2"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:scrollIndicators="none"
                        android:scrollbars="none">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical">

                            <FrameLayout
                                android:id="@+id/Pack1"
                                android:layout_width="fill_parent"
                                android:layout_height="fill_parent"
                                android:background="#e1e1e1"
                                android:paddingBottom="@dimen/Pack_Bottom"
                                android:visibility="visible">

                                <LinearLayout
                                    android:id="@+id/linear1"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:orientation="vertical">



                                    <ImageView
                                        android:id="@+id/listPrvy"
                                        android:layout_width="match_parent"
                                        android:layout_height="0dp"
                                        android:clickable="true"
                                        android:longClickable="true"
                                        android:scaleType="fitXY"/>
                                </LinearLayout>

                            </FrameLayout>
                            </LinearLayout>
                            </ScrollView>

I call method ↑ (which write bitmap into imageview)

itemimg = new ItemsInPacagesImageView(imglist1, tentocontext, nazovtripu, 0, prvy); - this class it's mine it's method which put bitmap into imglist1 (imageview).

When i try put b.recycle(); after Bitmap b = Bitmap.createBitmap(srk, vsk, Bitmap.Config.ARGB_8888);

        c = new Canvas(b);
        imageview.setImageBitmap(b);

application crashed with something like using recycled bitmap.

Thanks for any idea.

Image

stairs it's after start scrolling... (25 items width 1080 , height 5040px)

Upvotes: 0

Views: 147

Answers (2)

ismail alaoui
ismail alaoui

Reputation: 6073

One of the common issue on android come when you start dealing with images , it causes a lot outofmemoryerror and specialize with a high resolution pictures.

if you want to avoid any outofmemoryerror issue on android causing by Image , i advice you to use Picasso : Link

Upvotes: 0

rupashka
rupashka

Reputation: 399

Bitmaps take a lot of memory. Some tips are:

  1. Avoid Bitmap.Config.ARGB_8888. For opaque images use Bitmap.Config.RGB_565
  2. Set BitmapFactory.Options.inPurgeable = true for your images. It's deprecated in last Android versions but works fine in old ones

Upvotes: 0

Related Questions