1

I'm using ViewFlipper, my viewFlipper was working properly till I increased the height of pictures.

It gives me OutOfMemoryError.

Here is my XML code for activity containing viewFlipper:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.taktaz.activities.OneProductActivity"
>
<ViewFlipper
    android:id="@+id/flipper1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="450px"
    android:clipChildren="true"
    android:autoStart="true"
    android:flipInterval="4000"
    android:inAnimation="@anim/right_in"
    android:outAnimation="@anim/left_out"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentStart="true">


</ViewFlipper>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="vertical"
    android:layout_alignParentBottom="true"
    android:layout_margin="4dp"
    >
    <Button
        android:id="@+id/btn_productDetails"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_product_specifics_style"
        android:layout_marginBottom="1dp"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/btn_productCatalog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_product_catalog_style"
        android:layout_marginBottom="1dp"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/btn_productManual"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_product_manual_style"
        android:layout_marginBottom="1dp"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/btn_productWebsite"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_product_website_style"
        android:layout_marginBottom="1dp"
        android:layout_weight="1"
        />
</LinearLayout>

and here i set the viewFlipper ImageResources:

 private void setUpFlipper() {
    TypedArray imgResource = getResources().obtainTypedArray(R.array.hayabusa_imgs);
    mFlipper = (ViewFlipper) findViewById(R.id.flipper1);

    for (int i = 0 ; i < imgResource.length() ; i++)
    {
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(imgResource.getResourceId(i,-1));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setAdjustViewBounds(true);
        mFlipper.addView(imageView);
    }
}

i didn't use Bitmaps.

when I remove buttons, viewFlipper works without errors, but when I put these 4 buttons bellow the flipper it not works, and will close with OutOfMemoryError.

What should I do?

Ali_kabir
  • 19
  • 4
  • It is highly unlikely that your layout is causing OOM errors. It is most likely caused by your unposted code. You should also post a stack trace as otherwise all anybody can do is guess. Also, see http://stackoverflow.com/help/how-to-ask – Kuffs Nov 27 '15 at 10:39
  • Easy solution: use LargeHeap on the manifest. – Nanoc Nov 27 '15 at 14:56

1 Answers1

2

It's a common issue with images in android:

http://developer.android.com/training/displaying-bitmaps/index.html

You consume android limit and that's why you see this error. Rescale images, don't show them in original size.

ViewFlipper loads all views in memory. It's a bad idea to load all images in memory. Take a look at the following question How many Views are possible in a view flipper in android?

Since Gallery is deprecated use ViewPager. ViewPager is available in Support library. You can also configure how many views you should load in both directions forward and backward. You'll load images on demand as well as destroy them as long as they are not currently needed. It allows you to reuse Views and load only several views in memory:

http://codetheory.in/android-image-slideshow-using-viewpager-pageradapter/

Here you can find some good examples how to implement it: Android Viewpager as Image Slide Gallery

Community
  • 1
  • 1
Access Denied
  • 8,723
  • 4
  • 42
  • 72