从SD卡加载图像时的gridview和setImageBitmap

时间:2011-08-11 13:20:43

标签: android image gridview sd-card

我想创建一个gridview来从SD卡加载图像。我从互联网上找到了很多信息,但我无法成功。

当我运行以下代码时,它会强行关闭。

我发现问题是imageView.setImageBitmap(bmp);

但我不知道这段代码有什么不妥。

我希望有人能帮助我解决这个问题。

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

错误日志:

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:6942)
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/>

2 个答案:

答案 0 :(得分:2)

在网格视图中,您不需要原始分辨率图像来显示SD卡中的图像。您始终可以使用BitmapFactory.Options.inSampleSize来获取较小尺寸的图像。这不会导致任何内存异常。

BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inSampleSize = 5; //any value greater than 1
Bitmap bmp = BitmapFactory.decodeFile(dir.getPath() + "/" + fileNames[position],opt);

答案 1 :(得分:0)

  

java.lang.OutOfMemoryError:位图大小超过VM预算。

根据您发布的错误,您的图片文件似乎太大了。它需要多少内存?尝试优化图像。