2011-10-18 2 views
0

디렉토리에서 이미지를 표시하는 listview가 있습니다. 그러나 이미지를 잘로드하고 있지만 화면을 스크롤 한 후 위로 스크롤하려면 화면에서 손가락을 떼어내어 다시 배치해야합니다. 화면을 다시 스크롤하십시오. 또한 스크롤이 너무 빠르면 logcat이 OutOfMemoryError라는 어플리케이션 크래시가 발생합니다. 하나의 이유가 될 것이다 당신의 도움이 일어나고에 관한 단서를 가지고imageview가 충돌을 계속합니다

package com.search.visual; 


import java.io.File; 
import java.io.FilenameFilter; 

import android.app.ListActivity; 
import android.content.Context; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.ListView; 


public class history extends ListActivity{ 

private String dir = ".vis"; 
private File images; 
private File [] imageList; 
private File item; 
private Uri [] ImageRef; 
private ImageView imageView; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.history); 




    loadPics(); 



} 



public class ImageAdapter extends BaseAdapter { 
    int Background; 
    private Context con; 



    public ImageAdapter(Context c) { 
     con = c; 
     TypedArray attr = con.obtainStyledAttributes(R.styleable.HelloGallery); 
     Background = attr.getResourceId(
       R.styleable.HelloGallery_android_galleryItemBackground, 0); 
     attr.recycle(); 
    } 

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

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

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

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



     imageView = new ImageView(con); 
     imageView.setImageURI(ImageRef[position]); 
     imageView.setLayoutParams(new ListView.LayoutParams(75, 75)); 
     imageView.setScaleType(ImageView.ScaleType.FIT_XY); 

     imageView.setBackgroundResource(Background); 

     return imageView; 
    } 


} 

protected void onListItemClick(ListView l, View v, int position, long id) { 

    item = imageList[position]; 
    Intent i = new Intent("com.search.visual.IMAGE"); 

     i.putExtra(Intent.EXTRA_STREAM, item); 
     startActivity(i);   //StIntring imname = (String) ; 
    } 





@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    loadPics(); 
    super.onResume(); 
} 

public void loadPics(){ 

    images = Environment.getExternalStoragePublicDirectory(dir); 
    imageList = images.listFiles(new FilenameFilter() { 

     public boolean accept(File dir, String filename) { 
      // TODO Auto-generated method stub 
      return (filename.endsWith(".jpg")); 
     } 
    }); 

    if(imageList.length > 0){ 
    ImageRef = new Uri[imageList.length]; 

    for(int i = 0; i<imageList.length; i++){ 
     ImageRef[i] = Uri.parse(imageList[i].getAbsolutePath()); 

    } 

    setListAdapter(new ImageAdapter(this)); 
    }else{ 
     Intent i = new Intent("com.search.visual.ERROR"); 
     startActivity(i); 
    } 
} 


} 

누구 경우 모든 이미지가 native heap에 저장되어있는 목록보기를 스크롤하면 크게

답변

1

감사 : 여기

내 코드입니다 recycler of native heap is too lazy. 따라서 목록보기에서 스크롤 된 이미지로 인해 수집 쓰레기가 느려지고 그 이유가 device's heap is become full이고 OutOfMemory 예외가 발생합니다.

내 생각에 당신은 sholud try your own recycler 이미지가 응용 프로그램의 이미지와 free the memory 이미지가 사용 된 것 같습니다.

비트 맵을 재활용해야합니다. 비트 맵 구현은 원시이므로 Java 객체는 작고 Java 가비지 콜렉션에 대한 후보가 좋지 않지만 메모리는 여전히 할당됩니다. 보기를 Bitmap.recycle()

또는 그렇지 않으면 Lazzy Loading images in Listview로 시도하십시오.

Android: Strange out of memory issueOutOfMemory exception appears while scrolling the list of images

감사합니다.

+0

'Uri Array'를 '비트 맵 배열'로 바꾸면 메모리 문제가 사라진 것처럼 보였고 스크롤 문제는 도움과 조언에 감사드립니다. –

관련 문제