2012-04-17 5 views
0

이클립스에서 안드로이드 갤러리를 만드는 동안이 오류가 발생합니다. 두 개의 이미지 만 추가하면 잘 실행되지만 갤러리에 더 많은 이미지를 추가하면이 오류가 발생합니다. 어떤 도움을 주시면 감사하겠습니다!안드로이드에서 갤러리를 만들 때 메모리 부족 오류

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; 
    } 
} 
} 
+0

이 질문과 대답을 확인하십시오. http://stackoverflow.com/questions/10161079/out-of-memory-exception-in-android-when-adding-some-image-buttons/10161107#10161107 – erbsman

+0

오류 로그를 제공 할 수 있습니까? –

답변

0

에서 OutOfMemory 비트 맵의 ​​많은 수를 사용할 때 오는 일반적인 오류가 발생하는 used.The 오류를 줄일 수있는 유일한 방법은 System.gc();를 호출하는 것입니다. System.gc() 코드에서 Java VM은 런타임에 가비지 콜렉션을 수행할지 여부를 결정할 수도 있고 결정하지 않을 수도 있습니다. 작업을 완료 한 후 비트 맵에 "Bitmap".recycle()로 전화를 걸어보십시오. 이렇게하면이 오류를 최대한 줄이는 데 도움이됩니다.

+0

이 솔루션은 작동하지 않습니다. 이 예외가 발생하는 이유를 제대로 파악하려면 다음 언급 된 스레드를 방문하십시오. http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object/823966#823966 –