Android Studio
(Android Studio) 앱 데이터 삭제 방법
SAFE
2016. 5. 20. 11:04
인터넷을 이용하는 앱을 만들다 보면 앱 안에 데이터가 계속 쌓여서 앱 크기가 엄청 늘어난다.
삭제 해줘야 할 때가 있다. 그래서 찾아보았다.
출처1 : http://www.androidside.com/bbs/board.php?bo_table=B49&wr_id=154234
출처2 : http://stackoverflow.com/questions/4856955/how-to-programmatically-clear-application-data
출처 1을 보고 영감을 얻고 출처 2의 코드를 찾았다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | // 데이터 삭제 public void clearApplicationData() { File cache = getCacheDir(); try { } catch (Exception e) { } File appDir = new File(cache.getParent()); if (appDir.exists()) { String[] children = appDir.list(); for (String s : children) { if (!s.equals("lib") && !(s.equals("shared_prefs"))) { 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; } } } // The directory is now empty or this is a file so delete it return dir.delete(); } | cs |
저걸 사용하려면?
1 2 | // 데이터 기록 삭제 clearApplicationData(); |
이걸 쓰면 된다.