2013-10-29 84 views
0

我想显示6个图像,我已将其保存在gridview中的res/drawable文件夹中。尝试在gridview中显示图像时发生java.lang.outofmemoryerror异常

,但我得到以下错误

10-29 09:44:20.025: E/AndroidRuntime(1549): FATAL EXCEPTION: main 
10-29 09:44:20.025: E/AndroidRuntime(1549): java.lang.OutOfMemoryError 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:501) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:354) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.content.res.Resources.loadDrawable(Resources.java:1965) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.content.res.Resources.getDrawable(Resources.java:660) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.widget.ImageView.resolveUri(ImageView.java:616) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.widget.ImageView.setImageResource(ImageView.java:349) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at com.shiva.bajarangi.ImageAdapter.getView(ImageAdapter.java:55) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.widget.AbsListView.obtainView(AbsListView.java:2159) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.widget.GridView.makeAndAddView(GridView.java:1341) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.widget.GridView.makeRow(GridView.java:341) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.widget.GridView.fillDown(GridView.java:283) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.widget.GridView.fillFromTop(GridView.java:417) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.widget.GridView.layoutChildren(GridView.java:1229) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.widget.AbsListView.onLayout(AbsListView.java:1994) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.view.View.layout(View.java:14008) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.view.ViewGroup.layout(ViewGroup.java:4373) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.view.View.layout(View.java:14008) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.view.ViewGroup.layout(ViewGroup.java:4373) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.view.View.layout(View.java:14008) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.view.ViewGroup.layout(ViewGroup.java:4373) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.view.View.layout(View.java:14008) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.view.ViewGroup.layout(ViewGroup.java:4373) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.view.View.layout(View.java:14008) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.view.ViewGroup.layout(ViewGroup.java:4373) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.view.Choreographer.doCallbacks(Choreographer.java:562) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.view.Choreographer.doFrame(Choreographer.java:532) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.os.Handler.handleCallback(Handler.java:725) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.os.Handler.dispatchMessage(Handler.java:92) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.os.Looper.loop(Looper.java:137) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at android.app.ActivityThread.main(ActivityThread.java:5041) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at java.lang.reflect.Method.invoke(Method.java:511) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
10-29 09:44:20.025: E/AndroidRuntime(1549):  at dalvik.system.NativeStart.main(Native Method) 

这是我的适配器类

public class ImageAdapter extends BaseAdapter { 
    Context mcontext; 
    Integer Imageid[] = new Integer[] { R.drawable.image1, R.drawable.image3, 
      R.drawable.image4, R.drawable.image5, R.drawable.image6, 
      R.drawable.image2 }; 



    public ImageAdapter(Context context) { 
     // TODO Auto-generated constructor stub 
     super(); 
     this.mcontext = context; 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return Imageid.length; 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 

     LayoutInflater inflater = (LayoutInflater) mcontext 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View gridView; 
     if(convertView == null){ 
      gridView = new View(mcontext); 

      gridView = inflater.inflate(R.layout.imageview,null); 

      ImageView imageView =(ImageView)gridView.findViewById(R.id.imageView); 
      imageView.setImageResource(Imageid[position]); 
     } 
      else { 
       gridView = (View) convertView; 
      } 


     return gridView; 
    } 

} 

这是为什么错误?..提前

谢谢:)

+0

你的图片有多大? –

+0

@ A.S。 135kb,190kb,73.7kb,154kb,268kb和500kb。是每个图像的图像大小 – Beginner

+1

负载按比例缩小图像到GridView作为原始图像消耗大量内存 –

回答

4

这里发生的事情是,该应用程序将超出堆大小。因此,您必须以这种方式解码图像,使其落入堆大小。请在适配器中执行此代码

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 

     LayoutInflater inflater = (LayoutInflater) mcontext 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View gridView; 
     if(convertView == null){ 
      gridView = new View(mcontext); 

      gridView = inflater.inflate(R.layout.imageview,null); 
      ImageView imageView =(ImageView)gridView.findViewById(R.id.imageView); 

      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inDither = false; 
      options.inJustDecodeBounds = false; 
      options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
      options.inSampleSize = 3; 
      options.inPurgeable = true; 

      Bitmap icon = BitmapFactory.decodeResource(context.getResources(), 
        Imageid[position],options); 


      holder.imageView.setImageBitmap(icon); 
     } 
      else { 
       gridView = (View) convertView; 
      } 


     return gridView; 
    } 
+0

伟大的答案我也有同样的问题,但现在解决谢谢@Akhilesh Sk – Simmant

+0

我需要在你的代码做一些小的改变..它为我工作..谢谢: ) – Beginner

0

的你使用的图像很大。
看一看这个article

缩放您的图片。 - 减少它们的尺寸和质量。

相关问题