ViewPager有5张图片768x1024会导致OutOfMemory

时间:2014-04-10 14:07:03

标签: android imageview out-of-memory drawable

我需要将所有5张图片放在5个不同的页面上,但是第2张导致内存不足。 这是我的适配器:

public class InfoAdapter extends BaseAdapter   {

    private LayoutInflater mInflater;
    private static final int[] ids = { R.drawable.easter1, R.drawable.easter2, R.drawable.easter3, R.drawable.easter4,
            R.drawable.easter5 };
    Context context;


    public InfoAdapter(Context context)  {
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.context=context;
        }

所以,ids [] - 是一个R.id的数组

这里是getView:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Drawable drawable=context.getResources().getDrawable(ids[position]);
        if (position!=2){
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.info_item, null);
        }
        ((ImageView) convertView.findViewById(R.id.infoImage)).setImageDrawable(drawable);


        return convertView;
    }
        else {
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.info_map, null);
                ((ImageView) convertView.findViewById(R.id.infoImage)).setImageDrawable(drawable);
                ((ImageView) convertView.findViewById(R.id.map_area)).setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                    Intent intent=new Intent(context,MapActivity.class);
                    context.startActivity(intent);

                    }
                });
            }


            return convertView;



}

如何避免使用outOFMemory并使其正常工作?

错误:

04-10 18:57:16.557: E/AndroidRuntime(14817): FATAL EXCEPTION: main
04-10 18:57:16.557: E/AndroidRuntime(14817): java.lang.OutOfMemoryError
04-10 18:57:16.557: E/AndroidRuntime(14817):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
04-10 18:57:16.557: E/AndroidRuntime(14817):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
04-10 18:57:16.557: E/AndroidRuntime(14817):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
04-10 18:57:16.557: E/AndroidRuntime(14817):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
04-10 18:57:16.557: E/AndroidRuntime(14817):    at android.content.res.Resources.loadDrawable(Resources.java:2007)
04-10 18:57:16.557: E/AndroidRuntime(14817):    at android.content.res.Resources.getDrawable(Resources.java:673)
04-10 18:57:16.557: E/AndroidRuntime(14817):    at ru.nikeabooks.eastermessage.InfoAdapter.getView(InfoAdapter.java:65)
04-10 18:57:16.557: E/AndroidRuntime(14817):    at org.taptwo.android.widget.ViewFlow.obtainView(ViewFlow.java:785)
04-10 18:57:16.557: E/AndroidRuntime(14817):    at org.taptwo.android.widget.ViewFlow.makeAndAddView(ViewFlow.java:779)
04-10 18:57:16.557: E/AndroidRuntime(14817):    at org.taptwo.android.widget.ViewFlow.postViewSwitched(ViewFlow.java:724)
04-10 18:57:16.557: E/AndroidRuntime(14817):    at org.taptwo.android.widget.ViewFlow.access$7(ViewFlow.java:705)
04-10 18:57:16.557: E/AndroidRuntime(14817):    at org.taptwo.android.widget.ViewFlow$2.run(ViewFlow.java:539)
04-10 18:57:16.557: E/AndroidRuntime(14817):    at android.os.Handler.handleCallback(Handler.java:725)
04-10 18:57:16.557: E/AndroidRuntime(14817):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-10 18:57:16.557: E/AndroidRuntime(14817):    at android.os.Looper.loop(Looper.java:137)
04-10 18:57:16.557: E/AndroidRuntime(14817):    at android.app.ActivityThread.main(ActivityThread.java:5102)
04-10 18:57:16.557: E/AndroidRuntime(14817):    at java.lang.reflect.Method.invokeNative(Native Method)
04-10 18:57:16.557: E/AndroidRuntime(14817):    at java.lang.reflect.Method.invoke(Method.java:511)
04-10 18:57:16.557: E/AndroidRuntime(14817):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-10 18:57:16.557: E/AndroidRuntime(14817):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

所以错误行是:

Drawable drawable=context.getResources().getDrawable(ids[position]);

1 个答案:

答案 0 :(得分:0)

尝试阅读this。和this。您必须缩小位图并显示样本,因为无法一次显示该大小的所有图像。

编辑:试试这段代码并告诉我你得到了什么。

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {

           if (position!=2)
              convertView = mInflater.inflate(R.layout.info_item, null);
           else {
              convertView = mInflater.inflate(R.layout.info_map, null);
           ((ImageView) convertView.findViewById(R.id.map_area)).setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                    Intent intent=new Intent(context,MapActivity.class);
                    context.startActivity(intent);
                    }
                });
            }

            holder.info_image = (ImageView) convertView.findViewById(R.id.infoImage);
            convertView.setTag(holder);
        } else {
           holder = (Holder)convertView.getTag();
        }

        Drawable drawable = context.getResources().getDrawable(ids[position]);
        holder.info_image.setImageDrawable(drawable);

        return convertView;
    }
}

private static class Holder {
public ImageView info_image;
}