开发者

gridview and setImageBitmap when load image from SDcard

开发者 https://www.devze.com 2023-03-27 15:43 出处:网络
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.

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<br/>
08-11 23:52:49.710: ERROR/dalvikvm-heap(5395): 10036224-byte external allocation too large for this process.<br/>
08-11 23:52:49.710: ERROR/(5395): VM won't let us allocate 10036224 bytes.<br/>
08-11 23:52:49.710: ERROR/AndroidRuntime(5395): Uncaught handler: thread main exiting due to uncaught exception.<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395): java.lang.OutOfMemoryError: bitmap size exceeds VM budget.<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:271).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:296).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at com.easyhome.GridActivity$ImageAdapter.getView(GridActivity.java:81).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.widget.AbsListView.obtainView(AbsListView.java:1291).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.widget.GridView.makeAndAddView(GridView.java:1218).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.widget.GridView.makeRow(GridView.java:265).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.widget.GridView.fillDown(GridView.java:218).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.widget.GridView.fillFromTop(GridView.java:337).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.widget.GridView.layoutChildren(GridView.java:1112).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.widget.AbsListView.onLayout(AbsListView.java:1130).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.view.View.layout(View.java:6942).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.widget.LinearLayout.onLayout(LinearLayout.java:918).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.view.View.layout(View.java:6942).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.view.View.layout(View.java:6942).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.widget.LinearLayout.onLayout(LinearLayout.java:918).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.view.View.layout(View.java:6开发者_如何学C942)
08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.view.View.layout(View.java:6942).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1007).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1656).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.os.Handler.dispatchMessage(Handler.java:99).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.os.Looper.loop(Looper.java:136).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at android.app.ActivityThread.main(ActivityThread.java:4425).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at java.lang.reflect.Method.invokeNative(Native Method).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at java.lang.reflect.Method.invoke(Method.java:521).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:850).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608).<br/>

08-11 23:52:49.764: ERROR/AndroidRuntime(5395):     at dalvik.system.NativeStart.main(Native Method).<br/>


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);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消