gridview and setImageBitmap when load image from SDcard-Collection of common programming errors

I want to create a gridview to load image from SD card. I find many information from the internet but I cannot success to do so.

When i run the below code, it forces to close.

I find that the problem is imageView.setImageBitmap(bmp);

But i do not know what wrong of this code.

I hope someone can help me to solve this problem.

public class GridActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_activity); 
        GridView gridview = (GridView)findViewById(R.id.GridView01);    
        gridview.setAdapter(new ImageAdapter(this));    
        gridview.setOnItemClickListener(new OnItemClickListener() {        
            public void onItemClick(AdapterView parent, View v, int position, long id) {            
                Toast.makeText(GridActivity.this, "" + position, Toast.LENGTH_SHORT).show();        
                }    
            });
        }

    public class ImageAdapter extends BaseAdapter {    
           File dir=new File(Environment.getExternalStorageDirectory(),"/images/bedroom");  
           int count=dir.list().length;   
           String[] fileNames = dir.list();  
           private Context mContext; 
        public ImageAdapter(Context c) {        
            mContext = c;    
            }    
        public int getCount() {        
            return count;    
            }    
        public Object getItem(int position) {        
            return null;    
            }    
        public long getItemId(int position) {        
            return 0;    
            }     
        public View getView(int position, View convertView, ViewGroup parent) {        
            ImageView imageView = null; 

            if (convertView == null) {  
                // if it's not recycled, initialize some attributes            
                imageView = new ImageView(mContext);            
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));            
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);            
                imageView.setPadding(8, 8, 8, 8);
                } else {
                imageView = (ImageView) convertView;        
                }
            Bitmap bmp = BitmapFactory.decodeFile(dir.getPath() + "/" + fileNames[position]);
            imageView.setImageBitmap(bmp);
            toast(dir.getPath() + "/" + fileNames[position]);
            return imageView;    
        }

        public View getView1(int arg0, View arg1, ViewGroup arg2) {
            // TODO Auto-generated method stub
            return null;
        }
    }

    public void toast (String msg)
    {
        Toast.makeText (getApplicationContext(), msg, Toast.LENGTH_SHORT).show ();
    }
}

Error Log :

08-11 23:52:38.874: ERROR/MBGlossaryManager(5395): lib_glossary.so can't be loaded
08-11 23:52:49.710: ERROR/dalvikvm-heap(5395): 10036224-byte external allocation too large for this process.
08-11 23:52:49.710: ERROR/(5395): VM won't let us allocate 10036224 bytes.
08-11 23:52:49.710: ERROR/AndroidRuntime(5395): Uncaught handler: thread main exiting due to uncaught exception.
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): java.lang.OutOfMemoryError: bitmap size exceeds VM budget.
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:271).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:296).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at com.easyhome.GridActivity$ImageAdapter.getView(GridActivity.java:81).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.AbsListView.obtainView(AbsListView.java:1291).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.GridView.makeAndAddView(GridView.java:1218).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.GridView.makeRow(GridView.java:265).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.GridView.fillDown(GridView.java:218).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.GridView.fillFromTop(GridView.java:337).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.GridView.layoutChildren(GridView.java:1112).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.AbsListView.onLayout(AbsListView.java:1130).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.view.View.layout(View.java:6942).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.LinearLayout.onLayout(LinearLayout.java:918).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.view.View.layout(View.java:6942).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.FrameLayout.onLayout(FrameLayout.java:333).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.view.View.layout(View.java:6942).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.LinearLayout.onLayout(LinearLayout.java:918).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.view.View.layout(View.java:6942) 08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.FrameLayout.onLayout(FrameLayout.java:333).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.view.View.layout(View.java:6942).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.view.ViewRoot.performTraversals(ViewRoot.java:1007).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.view.ViewRoot.handleMessage(ViewRoot.java:1656).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.os.Handler.dispatchMessage(Handler.java:99).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.os.Looper.loop(Looper.java:136).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.app.ActivityThread.main(ActivityThread.java:4425).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at java.lang.reflect.Method.invokeNative(Native Method).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at java.lang.reflect.Method.invoke(Method.java:521).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:850).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608).
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at dalvik.system.NativeStart.main(Native Method).
  1. In a grid view you do not need the original resolution image to display the images from the sdcard. You can always use the BitmapFactory.Options.inSampleSize to get a smaller sized image. This will not result in any memory exceptions.

    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inSampleSize = 5; //any value greater than 1
    Bitmap bmp = BitmapFactory.decodeFile(dir.getPath() + "/" + fileNames[position],opt);
    
  2. java.lang.OutOfMemoryError: bitmap size exceeds VM budget.

    From the errors you posted it seems your image file is too large. How much memory does it require? Try optimizing the image.

Originally posted 2013-11-16 20:49:43.