🤖 안드로이드 Android
[안드로이드/Android] 앱 데이터&캐시 (App Data&Cache) 삭제
핑크빛연어
2021. 5. 11. 17:28
보통 안드로이드 앱 데이터&캐시 삭제는
설정 > 애플리케이션 > 해당 앱 의 저장공간 에서
데이터삭제, 캐시 삭제 버튼을 클릭하여 삭제할 수 있습니다.
소스코드를 통해 삭제하는 방법입니다.
AppData 삭제하는 메소드 clearAppData() 구현
public static void clearAppData(Context context) {
File cache = context.getCacheDir(); //캐시 폴더 호출
File appDir = new File(cache.getParent()); //App Data 삭제를 위해 캐시 폴더의 부모폴더까지 호출
if(appDir.exists()) {
String[] children = appDir.list();
for(String s : children) {
//App Data 폴더의 리스트를 deleteDir 를 통해 하위 디렉토리 삭제
deleteDir(new File(appDir, s));
}
}
}
public static boolean deleteDir(File dir) {
if(dir != null && dir.isDirectory()) {
String[] children = dir.list();
//파일 리스트를 반복문으로 호출
for(int i=0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if(!success) {
return false;
}
}
}
//디렉토리가 비어있거나 파일이므로 삭제 처리
return dir.delete();
}
clearAppData() 메소드 호출
//앱 데이터 삭제
clearAppData(MainActivity.this);
감사합니다 🤔
728x90
반응형