【问题标题】:Out of MemoryError when creating gallery on android在 android 上创建画廊时出现内存不足错误
【发布时间】:2012-04-17 10:32:44
【问题描述】:

在 Eclipse 中创建 android 库时出现此错误。当我只添加两个图像时,它运行良好,但是当我将更多图像添加到图库时,它会出现此错误。任何帮助将不胜感激!

package com.hospital;
import com.hospital.R;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;


import android.view.View;
import android.view.ViewGroup;

import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

import android.widget.AdapterView.OnItemClickListener;


public class myGallery extends Activity {

    private final Integer[] pic = {
            R.drawable.sample_1,
            R.drawable.sample_2,
            R.drawable.sample_3


    };
    ImageView imageview;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gallery);

        Gallery gallery = (Gallery)findViewById(R.id.pics);
        gallery.setAdapter(new ImageAdapter(this));

        imageview = (ImageView)findViewById(R.id.ImageView01);
        gallery.setOnItemClickListener(new OnItemClickListener() 
        {
            public void onItemClick(AdapterView <?> arg0, View arg1, int arg2, long arg3) {
                Toast.makeText(myGallery.this, "" + arg0, Toast.LENGTH_SHORT).show();
                imageview.setImageResource(pic[arg2]);
            }
        });
        registerForContextMenu(gallery);
    }

public class ImageAdapter extends BaseAdapter {
    private final int mGalleryItemBackground;
    private final Context mContext;
    private final Integer[] pic = {
            R.drawable.sample_1,
            R.drawable.sample_2,
            R.drawable.sample_3


    };

       public ImageAdapter(Context c) {
        mContext = c;
        TypedArray attr = c.obtainStyledAttributes(R.styleable.photoGallery);
        mGalleryItemBackground = attr.getResourceId(
                R.styleable.photoGallery_android_galleryItemBackground, 1);
        attr.recycle();
    }

    public int getCount() {
        return pic.length;
    }
    public Object getItem(int arg0) {
        return arg0;
    }

    public long getItemId(int arg0) {
        return arg0;
    }

    public View getView(int arg0, View arg1, ViewGroup arg2) {
        ImageView imageView = new ImageView(mContext);

        imageView.setImageResource(pic[arg0]);
        imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setBackgroundResource(mGalleryItemBackground);

        return imageView;
    }
}
}

【问题讨论】:

标签: android


【解决方案1】:

OutOfMemory 是使用大量位图时出现的常见错误。减少错误的唯一方法是在代码中调用 System.gc();.System.gc(),Java VM 可能会或可能不会在运行时决定执行那时的垃圾收集。完成与位图的业务后,尝试在位图上调用"Bitmap".recycle()。这将帮助您最大限度地减少此错误。

【讨论】:

【解决方案2】:

Google 提供了解决您遇到的问题的方法,您可以在以下链接中访问它。

http://developer.android.com/training/displaying-bitmaps/process-bitmap.html

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    • 2016-10-20
    相关资源
    最近更新 更多