gridView Android Eclipse中的内存不足错误

时间:2013-08-31 19:49:16

标签: java android eclipse gridview out-of-memory

我是Android开发人员的初学者。我正在尝试制作壁纸应用。显示2秒的启动画面,然后切换到我已显示15个图像的网格视图。许多错误显示何时转到显示网格布局的MainActivity。该应用程序约为5 MB。那是问题吗? 请帮助。

此活动开始后,这是LogCat。

09-01 01:07:05.134: E/dalvikvm-heap(6811): Out of memory on a 16384016-byte allocation.
09-01 01:07:05.144: E/AndroidRuntime(6811): FATAL EXCEPTION: main
09-01 01:07:05.144: E/AndroidRuntime(6811): java.lang.OutOfMemoryError
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:503)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:356)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:800)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.content.res.Resources.loadDrawable(Resources.java:2105)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.content.res.Resources.getDrawable(Resources.java:695)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.widget.ImageView.resolveUri(ImageView.java:636)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.widget.ImageView.setImageResource(ImageView.java:365)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at com.gamerspitch.dbzwallapper.ImageAdapter.getView(ImageAdapter.java:58)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.widget.AbsListView.obtainView(AbsListView.java:2177)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.widget.GridView.makeAndAddView(GridView.java:1341)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.widget.GridView.makeRow(GridView.java:341)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.widget.GridView.fillDown(GridView.java:283)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.widget.GridView.fillFromTop(GridView.java:417)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.widget.GridView.layoutChildren(GridView.java:1229)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.widget.AbsListView.onLayout(AbsListView.java:2012)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.view.View.layout(View.java:14289)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.view.ViewGroup.layout(ViewGroup.java:4559)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.view.View.layout(View.java:14289)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.view.ViewGroup.layout(ViewGroup.java:4559)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.view.View.layout(View.java:14289)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.view.ViewGroup.layout(ViewGroup.java:4559)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.view.View.layout(View.java:14289)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.view.ViewGroup.layout(ViewGroup.java:4559)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.view.Choreographer.doFrame(Choreographer.java:532)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.os.Handler.handleCallback(Handler.java:730)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.os.Looper.loop(Looper.java:137)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at java.lang.reflect.Method.invokeNative(Native Method)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at java.lang.reflect.Method.invoke(Method.java:525)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-01 01:07:05.144: E/AndroidRuntime(6811):     at dalvik.system.NativeStart.main(Native Method)

MainActivity.java

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.grid_layout);

    GridView gridView = (GridView) findViewById(R.id.grid_view);

    // Instance of ImageAdapter Class
    gridView.setAdapter(new ImageAdapter(this));

    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

            // Sending image id to FullScreenActivity
            Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
            // passing array index
            i.putExtra("id", position);
            startActivity(i);
        }
    });
}

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {

private Context mContext;

// Keep all Images in array
public Integer[] mThumbIds = { R.drawable.pic1, R.drawable.pic2, 
        R.drawable.pic3, R.drawable.pic4, R.drawable.pic5,
        R.drawable.pic6, R.drawable.pic7, R.drawable.pic8,
        R.drawable.pic9, R.drawable.pic10, R.drawable.pic11,
        R.drawable.pic12, R.drawable.pic13, R.drawable.pic14,
        R.drawable.pic15, R.drawable.pic16 };


public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) 
    {  
        Resources r = Resources.getSystem();
        float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 120, r.getDisplayMetrics());
        // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT, (int)px));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        //imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

}

grid_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background2"
android:padding="10dp"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth" >


</GridView>

请帮我解决这个问题,或者向我解释为什么我会收到这些错误。提前谢谢。

3 个答案:

答案 0 :(得分:2)

基本上,您在gridview中加载了TOOOOO个许多图片。我的建议是将你的照片放到网上,然后使用ImageLoader来缓存你的图像,这样他们也可以离线使用。它将提供平滑的滚动和更快的加载!

https://github.com/thest1/LazyList

答案 1 :(得分:1)

  1. 在线上传所有图片。不难。
  2. 然后将LazyList github项目中的所有类复制到您的应用程序。只需复制代码或创建一个新包并将类粘贴到那里。
  3. 现在您需要在此处修改此代码

  4.     public Integer[] mThumbIds = { R.drawable.pic1, R.drawable.pic2, 
                R.drawable.pic3, R.drawable.pic4, R.drawable.pic5,
                R.drawable.pic6, R.drawable.pic7, R.drawable.pic8,
                R.drawable.pic9, R.drawable.pic10, R.drawable.pic11,
                R.drawable.pic12, R.drawable.pic13, R.drawable.pic14,
                R.drawable.pic15, R.drawable.pic16 
    
    };
    

    public String[] avariable = {urlforimage1, urlforimage2,blabla};
    

    然后输入ImageLoader.displayImage(avariable[position], imageview);

    不要忘记构造函数。

答案 2 :(得分:1)

其他人给你的关于将图像放在服务器上的答案根本不正确。如果它在应用程序或服务器上并不重要,因为最后,您需要解码图像。

应用程序的大小与图像无关,因为它取决于它们的分辨率以及放置它们的位置。一个jpg文件可能比显示它所需的内存少得多,特别是如果它没有像素上的大量随机性。

例如,如果你将1000x1000图像放在“res / drawable”文件夹(mdpi)中,然后在xhdpi设备(如galaxy s3)上运行应用程序,则需要

(1000*2)*(1000*2)*4=16,000,000 bytes ~ 16MB per image

如果解码其中16个,则使用256,000,000个字节。这是正确的 - 256 MB ,这对于图像而言是很多RAM。

xxhdpi设备(如galaxy s4和HTC one)的情况会更糟:

16*(1000*3)*(1000*3)*4 = 576,000,000 ~ 576 MB ...
你能做什么?实际上我已经回答了一个非常相似的问题here。简而言之,您可以缩减到您需要的大小,和/或使用不同的配置。你也可以将图像放在drawable-xhdpi文件夹中。