-1

I have created a Gallery like this:

MainActivity:

public class MainActivity extends Activity {
    Gallery gallery;
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gallery = (Gallery) findViewById(R.id.gallery1);
        imageView = (ImageView) findViewById(R.id.imageView1);
        gallery.setAdapter(new ImageAdapter(this));


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

ImageAdapter.java:

    public class ImageAdapter extends BaseAdapter {

    private Context context;

    public ImageAdapter(Context context) {
        // TODO Auto-generated constructor stub
        this.context = context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return thumbsIds.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ImageView imageView = null;
        if (convertView == null) {
            imageView = new ImageView(context);
            imageView.setLayoutParams(new Gallery.LayoutParams(115, 100));
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(thumbsIds[position]);
        return imageView;
    }

    public Integer[] thumbsIds = {
            R.drawable.a,
            R.drawable.b,
            R.drawable.c,
            R.drawable.d
    };
}

and the activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

But when I start my emulator I receive this exception:

10-18 13:32:28.107: E/AndroidRuntime(715): FATAL EXCEPTION: main
10-18 13:32:28.107: E/AndroidRuntime(715): java.lang.OutOfMemoryError
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:483)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.content.res.Resources.loadDrawable(Resources.java:1937)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.content.res.Resources.getDrawable(Resources.java:664)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.widget.ImageView.resolveUri(ImageView.java:542)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.widget.ImageView.setImageResource(ImageView.java:315)
10-18 13:32:28.107: E/AndroidRuntime(715):  at com.example.gallerydemo.ImageAdapter.getView(ImageAdapter.java:46)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.widget.Gallery.makeAndAddView(Gallery.java:844)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.widget.Gallery.fillToGalleryRightLtr(Gallery.java:798)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.widget.Gallery.fillToGalleryRight(Gallery.java:742)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.widget.Gallery.layout(Gallery.java:651)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.widget.Gallery.onLayout(Gallery.java:346)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.view.View.layout(View.java:11180)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.view.ViewGroup.layout(ViewGroup.java:4203)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.view.View.layout(View.java:11180)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.view.ViewGroup.layout(ViewGroup.java:4203)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.view.View.layout(View.java:11180)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.view.ViewGroup.layout(ViewGroup.java:4203)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.view.View.layout(View.java:11180)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.view.ViewGroup.layout(ViewGroup.java:4203)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.view.View.layout(View.java:11180)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.view.ViewGroup.layout(ViewGroup.java:4203)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1468)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2418)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.os.Looper.loop(Looper.java:137)
10-18 13:32:28.107: E/AndroidRuntime(715):  at android.app.ActivityThread.main(ActivityThread.java:4340)
10-18 13:32:28.107: E/AndroidRuntime(715):  at java.lang.reflect.Method.invokeNative(Native Method)
10-18 13:32:28.107: E/AndroidRuntime(715):  at java.lang.reflect.Method.invoke(Method.java:511)
10-18 13:32:28.107: E/AndroidRuntime(715):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-18 13:32:28.107: E/AndroidRuntime(715):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-18 13:32:28.107: E/AndroidRuntime(715):  at dalvik.system.NativeStart.main(Native Method)

the app don't start itself. it crash immediately.

Suhail Mehta
  • 5,514
  • 2
  • 23
  • 37
Gabrio
  • 388
  • 1
  • 4
  • 17
  • 1) `Gallery` is [**deprecated**](http://developer.android.com/reference/android/widget/Gallery.html), don't use it. 2) You are encountering an `OutOfMemoryError` which means your App ran out of useable memory. Most likely you are loading too many pictures or too large pictures at once. 3) Always include the tag of the programming language you are using in your question. Otherwise the syntax highlighting will not work properly. – Xaver Kapeller Oct 18 '14 at 13:42
  • so i need to use horizontal scroll view right? – Gabrio Oct 18 '14 at 13:49
  • Use a `Listview`. You should really go through Google' tutorials on the subject.... – Xaver Kapeller Oct 18 '14 at 13:52
  • can u suggest me a good tutorial ? ps: i'm using an horizontal scroll view but i encouner the same error of OutOfMemory. how can i solve it? – Gabrio Oct 18 '14 at 14:12
  • Didn't I already do that? Go through Googles tutorials. You can find them [**here**](https://developer.android.com/training/index.html). – Xaver Kapeller Oct 18 '14 at 14:15
  • thank you Xaver, if u solve my last problem i will love u – Gabrio Oct 18 '14 at 14:17
  • Omg of course you get the same out of memory error with a scrollview.... Use a Listview. – Xaver Kapeller Oct 18 '14 at 14:17
  • You should really go through all of Googles tutorials before attempting to write your own app. – Xaver Kapeller Oct 18 '14 at 14:18

1 Answers1

0

this link may help and load the images in a separate thread using Asynctask for smooth scrolling. http://developer.android.com/training/displaying-bitmaps/load-bitmap.html.

Srishti Roy
  • 576
  • 5
  • 17