0

So I'm trying to build a gallery that displays images from a folder on the SD card not images in my drawable folder. I have an array of the file paths and I'm trying to use the file uri from each image in the gallery but I get a run time error that crashes the app at this line:

iView.setImageURI(Uri.fromFile(new File(files.get(arg0))));

Here is part of the error in LogCat: (it was huge so I didn't include the entire thing)

12-30 14:26:56.600: E/AndroidRuntime(4995): FATAL EXCEPTION: main
12-30 14:26:56.600: E/AndroidRuntime(4995): java.lang.OutOfMemoryError
12-30 14:26:56.600: E/AndroidRuntime(4995):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
12-30 14:26:56.600: E/AndroidRuntime(4995):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493)
12-30 14:26:56.600: E/AndroidRuntime(4995):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
12-30 14:26:56.600: E/AndroidRuntime(4995):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:738)
12-30 14:26:56.600: E/AndroidRuntime(4995):     at android.graphics.drawable.Drawable.createFromStream(Drawable.java:698)
12-30 14:26:56.600: E/AndroidRuntime(4995):     at android.widget.ImageView.resolveUri(ImageView.java:530)
12-30 14:26:56.600: E/AndroidRuntime(4995):     at android.widget.ImageView.setImageURI(ImageView.java:314)
12-30 14:26:56.600: E/AndroidRuntime(4995):     at com.cp.Media$ImageAdapter.getView(Media.java:174)
12-30 14:26:56.600: E/AndroidRuntime(4995):     at android.widget.Gallery.makeAndAddView(Gallery.java:748)
12-30 14:26:56.600: E/AndroidRuntime(4995):     at android.widget.Gallery.fillToGalleryRight(Gallery.java:700)
12-30 14:26:56.600: E/AndroidRuntime(4995):     at android.widget.Gallery.layout(Gallery.java:631)
12-30 14:26:56.600: E/AndroidRuntime(4995):     at android.widget.Gallery.onLayout(Gallery.java:339)

Below is my code, Does someone have any idea what I'm doing wrong?

    ImageSwitcher iSwitcher;
    private ArrayList<String> files = new ArrayList<String>();
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    setContentView(R.layout.media);
     files.add("/mnt/sdcard/DCIM/Camera/IMG_20111124_130713.jpg");
            files.add("/mnt/sdcard/DCIM/Camera/IMG_20111031_072817.jpg");
            files.add("/mnt/sdcard/DCIM/Camera/IMG_20111031_072750.jpg");
    iSwitcher = (ImageSwitcher) findViewById(R.id.ImageSwitcher01);
            iSwitcher.setFactory(this);
            iSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
                    android.R.anim.fade_in));
            iSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
                    android.R.anim.fade_out));

            Gallery gallery = (Gallery) findViewById(R.id.Gallery01);
            gallery.setAdapter(new ImageAdapter(this));
            gallery.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                    //iSwitcher.setImageResource(Uri.fromFile(new File(files.get(arg2))));
                    iSwitcher.setImageURI(Uri.fromFile(new File(files.get(arg2))));
                }
            });
    }

    public class ImageAdapter extends BaseAdapter {

            private Context ctx;

            public ImageAdapter(Context c) {
                ctx = c; 
            }

            @Override
            public int getCount() {

                return files.size();
            }

            @Override
            public Object getItem(int arg0) {

                return arg0;
            }

            @Override
            public long getItemId(int arg0) {

                return arg0;
            }

            @Override
            public View getView(int arg0, View arg1, ViewGroup arg2) {

                ImageView iView = new ImageView(ctx);

//THE LINE BELOW IS THE LINE THAT CAUSES THE ERROR IN LOGCAT
                iView.setImageURI(Uri.fromFile(new File(files.get(arg0))));
                iView.setScaleType(ImageView.ScaleType.FIT_XY);
                iView.setLayoutParams(new Gallery.LayoutParams(150, 150));
                return iView;
            }

        }

        @Override
        public View makeView() {
            ImageView iView = new ImageView(this);
            iView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            iView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
            iView.setBackgroundColor(0xFF000000);
            return iView;
        }
Peter
  • 5,071
  • 24
  • 79
  • 115

1 Answers1

1

the idea is to create a lighter bitmap from the file you have on the sdcard. take a look a this link: http://www.stackoverflow.com/a/823966/935075

sebataz
  • 1,025
  • 2
  • 11
  • 35
  • I'm sorry what does that mean? – Peter Dec 30 '11 at 20:53
  • the idea is to create a lighter bitmap from the file you have on the sdcard. take a look a this link: http://stackoverflow.com/a/823966/935075. – sebataz Dec 31 '11 at 10:24
  • Thank you the code from the SO question you linked to worked! Please revise your answer when you get a chance for others who may see this. Thanks again! – Peter Jan 01 '12 at 01:23