2012-08-31 3 views
2

Clear(); 다른 활동의 FileCache.class에 있습니다. 나는 나의 코딩의 작은 부분을 보여주고있다. 내 목표는 모든 출구에서 외부 캐시 된 파일을 지우는 것입니다. 누구든지 어떻게 완료되었는지 보여 주시겠습니까. 감사합니다Android 종료시 외부 이미지 캐시 지우기

FileCache.class

public class FileCache { 

    private File cacheDir; 

    public FileCache(Context context){ 
     //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(); 
    } 

    public File getFile(String url){ 
     //I identify images by hashcode. Not a perfect solution, good for the demo. 
     String filename=String.valueOf(url.hashCode()); 
     //Another possible solution (thanks to grantland) 
     //String filename = URLEncoder.encode(url); 
     File f = new File(cacheDir, filename); 
     return f; 

    } 

    public void clear(){ 
     File[] files=cacheDir.listFiles(); 
     if(files==null) 
      return; 
     for(File f:files) 
      f.delete(); 
    } 

} 

MyMainActivity.class

@Override 
     public void onBackPressed() { 
      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setMessage("Do you want to exit?") 
        .setCancelable(false) 
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 

          Intent intent = new Intent(Intent.ACTION_MAIN); 
          intent.addCategory(Intent.CATEGORY_HOME); 
          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
          startActivity(intent); 
          new clear(); //<<--- How do i Call clear(); in FileCache.class 
          System.exit(0); 
         } 
        }) 
        .setNegativeButton("No", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          dialog.cancel(); 
         } 
        }); 
      AlertDialog alert = builder.create(); 
      alert.show(); 

    } 
+0

이 도움이 될지 확인하십시오. http://stackoverflow.com/questions/2557570/solution-for-cleaning-an-image-cache-directory-on-the-sd-card –

답변

2

이보십시오.

public void onClick(DialogInterface dialog, int id) { 

           Intent intent = new Intent(Intent.ACTION_MAIN); 
           intent.addCategory(Intent.CATEGORY_HOME); 
           intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
           startActivity(intent); 
           FileCache loader = new FileCache(null); 
           loader.clear(); 
           System.exit(0); 
          } 
+0

감사합니다. 그 일 –

0

Lazy load of images in Android에 대한 해결책을 구현 했으리라 확신합니다. 이 같은

ImageLoader imgLoader = new ImageLoader(mContext); 
imgLoader.clearCache(); 
1

뭔가 :

public void clearCache() { 
     memoryCache.clear(); 
     fileCache.clear(); 
    } 

그래서 당신이 아래 위해 clearCache() 메서드를 호출하여 캐시를 지울 수 있습니다 :

이미 위해 clearCache하여 ImageLoader 클래스에 주어진() 메소드가있다?

public void onClick(DialogInterface dialog, int id) { 
    new FileCache(MyMainActivity.this).clear(); 
} 
0

FileCache 개체에 대한 참조가 필요합니다. 나는 당신이 활동에서 onCreate()에서 그것을 만든다고 가정합니다. 그렇다면 활동에서 FileCache를 속성으로 만드십시오. 그렇게하면 onBackPressed()에서 myFileCacheReference.clear()으로 전화 할 수 있습니다.

public class MainActivity extends Activity { 
    private FileCache myFileCacheRef; 

    public void onCreate(Bundle b) { 
    //standard stuff 
    myFileCacheRef = new FileCache(); 
    } 

    public void onBackPressed() { 
    myFileCacheRef.clear(); 
    } 
} 

이와 같이하면됩니다.