0

Hi I am trying to read images from all the folders in SD card. So its create folder list like download, camera etc. Now when I press any of the folder it open the Photos in the Grid view. My download folders has images of small size and the application works fine but when i open the camera folder which has bigger size images my application is crashing. My get view function of grid vies is as follows.

 public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);  
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.localimageview, null);
        }

        BitmapFactory.Options options=new Options();
        options.inSampleSize=2;
        options.inTempStorage = new byte[16*1024];

        Bitmap bb=BitmapFactory.decodeFile(FileList.get(position).toString(),options);
        i.setImageBitmap(bb);

        i.setLayoutParams(new GridView.LayoutParams(75,75));  
        i.setScaleType(ImageView.ScaleType.CENTER_CROP);
        i.setPadding(8, 8, 8, 8);
        v = (View)i;
        return v;  
    }
}  

}

Its crashing at
Bitmap bb=BitmapFactory.decodeFile(FileList.get(position).toString(),options);

    07-04 14:21:17.211: ERROR/AndroidRuntime(11509): FATAL EXCEPTION: main
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:271)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at com.ibm.imagegallery.ImageActivity$ImageAdapter.getView(ImageActivity.java:107)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.AbsListView.obtainView(AbsListView.java:1294)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.GridView.makeAndAddView(GridView.java:1219)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.GridView.makeRow(GridView.java:265)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.GridView.fillDown(GridView.java:218)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.GridView.fillFromTop(GridView.java:337)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.GridView.layoutChildren(GridView.java:1111)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.AbsListView.onLayout(AbsListView.java:1147)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.view.View.layout(View.java:7035)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.view.View.layout(View.java:7035)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.view.View.layout(View.java:7035)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.view.View.layout(View.java:7035)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.os.Handler.dispatchMessage(Handler.java:99)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.os.Looper.loop(Looper.java:123)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at java.lang.reflect.Method.invokeNative(Native Method)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at java.lang.reflect.Method.invoke(Method.java:521)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at dalvik.system.NativeStart.main(Native Method)
    07-04 14:21:17.221: WARN/ActivityManager(1378):   Force finishing activity com.ibm.imagegallery/.ImageActivity
    07-04 14:21:17.231: WARN/ActivityManager(1378):   Force finishing activity com.ibm.imagegallery/.ImageGallery
jhon
  • 87
  • 2
  • 10
  • Also, you should accept answers to your questions if you've found them to be useful(See there is a tick there)and also use upvotes. It will help you get more answers for your questions – Rishabh Jul 07 '11 at 11:14

3 Answers3

0

Check for example this question. Or do a search, this is a common problem answered before.

Community
  • 1
  • 1
0

When You access the camera pictures the once which get loaded are too large to handle for the application what you need to do is that you need to degrade the images and get it on your activity a solution to this problem can be taken from here Strange out of memory issue while loading an image to a Bitmap object

Community
  • 1
  • 1
Rishabh
  • 3,752
  • 4
  • 47
  • 74
0

Try following code.Hope this help

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            ViewWrapper wrapper = null;

            try {
                if (row == null) {
                    LayoutInflater inflater = getLayoutInflater();
                    row = inflater.inflate(R.layout.gallery_template, parent,
                            false);
                    wrapper = new ViewWrapper(row);
                    row.setTag(wrapper);
                } else {
                    wrapper = (ViewWrapper) row.getTag();
                }

                Bitmap bitmap = getImage(position);

                if (bitmap != null ) {

                    wrapper.getIcon().setImageBitmap(bitmap);
                } else {
                                  showErrorDialogue(getString(R.string.noGalleryData));
                }
            } catch (Exception e) {}
            return (row);
        }

private Bitmap getImage(int position) {
        Bitmap bitmap = null;
        if (Constants.IMAGES_PATH.size() > 0) {

            try {
  BitmapFactory.Options options = new BitmapFactory.Options();
  options.inSampleSize = 4;


                String path = Constants.IMAGES_PATH.get(position);
                // -------- Decode THE BITMAP AND RESIZE IT
                bitmap = BitmapFactory
                    .decodeFile(path,options));
            } catch (Exception e) {}
        }
        return bitmap;
    }
Sam
  • 322
  • 1
  • 6