2011-08-12 7 views
1

단일 버튼 클릭으로 캐시를 지울 수있는 테스트 응용 프로그램에서 fedor의 lazy loading list 구현을 사용하고 있습니다. 로드 된 이미지의 캐시 크기를 listview에서 가져 와서 프로그래밍 방식으로 캐시를 지우려면 어떻게해야합니까? 여기 안드로이드에서 캐시 크기를 얻는 방법

캐시 된 이미지를 저장하기위한 코드입니다

public ImageLoader(Context context){ 
    //Make the background thead low priority. This way it will not affect the UI performance. 
    photoLoaderThread.setPriority(Thread.NORM_PRIORITY-1); 
    mAssetManager = context.getAssets(); 

    //Find the dir to save cached images 
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) 
     cacheDir = new File(android.os.Environment.getExternalStorageDirectory(),"LazyList"); 
    else 
     cacheDir = context.getCacheDir(); 
    if(!cacheDir.exists()) 
     cacheDir.mkdirs(); 
} 

편집 : 나는 위해 clearCache() 메소드에이 코드 조각을 추가,하지만 난 여전히 볼 수 없습니다 그래서 기본적으로

스크롤 할 때 이미지가 다시로드되기 시작합니다.

public void clearCache() { 
    //clear memory cache 

    long size=0; 
    cache.clear(); 

    //clear SD cache 
    File[] files = cacheDir.listFiles(); 
    for (File f:files) { 
     size = size+f.length(); 
     if(size >= 200) 
      f.delete(); 
    } 
} 

답변

3

캐시 디렉토리의 크기를 찾으려면 codebelow를 사용하십시오.

public void clearCache() { 
    //clear memory cache 

    long size = 0; 
    cache.clear(); 

    //clear SD cache 
    File[] files = cacheDir.listFiles(); 
    for (File f:files) { 
     size = size+f.length(); 
     f.delete(); 
    } 
} 

이렇게하면 바이트 수가 반환됩니다.

+0

난 그냥 이미지가 아래로 스크롤 한 후로드 볼 수 없습니다 아직도 내가 지금 사용하고 코드 내 질문을 편집 할 수 있지만. –

+0

어디에서 캐시를 비우고 있습니까? 왜이 코드를 사용하는지 (size> = 200) f.delete(); –

+0

다음과 같은 주요 활동에서 이것을 호출합니다. adapter.imageLoader.clearCache(); adapter.notifyDataSetChanged(); . 그리고 IF를 넣습니다. 왜냐하면 크기가 아마도 200kb에 도달 할 때 캐시를 삭제하기를 원하기 때문입니다. 그게 내가하고있는 일인가요? –

0

... 캐시를 지우려면 단지 delete the directory을 입력하고 빈 것을 다시 만드십시오.

1

것은이 나에게 더 정확하고있다 :

private void initializeCache() { 
    long size = 0; 
    size += getDirSize(this.getCacheDir()); 
    size += getDirSize(this.getExternalCacheDir()); 
} 

public long getDirSize(File dir){ 
    long size = 0; 
    for (File file : dir.listFiles()) { 
     if (file != null && file.isDirectory()) { 
      size += getDirSize(file); 
     } else if (file != null && file.isFile()) { 
      size += file.length(); 
     } 
    } 
    return size; 
} 
관련 문제