안녕하세요
저는 웹뷰를 이용한 하이브리드 어플을 개발중이구요
오늘 하려는 어플 종료시 어플에서 다운로드한 파일 삭제하기는
제가 어플을 만드는 이유이기도 합니다.
웹은 이미 사용되는 중이었으나 고객이 원한건 웹에서는 cache를 지울수 없기 때문이었습니다.
저는 파일 업로드 및 다운로드 처리를 구현한 상태였구요
제가 다운로드한 path는 내장저장소의 개별어플 안 다운로드폴더 였습니다.
안드로이드의 업데이트로 내장저장소의 개별어플 내부의 디렉토리를 볼 수 없게 된 상태라는 것을 알고
살짝 멘붕에 빠지려던 찰나
캐시파일은 삭제가 가능하다는 document를 보고 다운로드path를 변경했습니다.
어플 종료시 작동하는 메소드로 onDestroy()부터 작성합니다.
public void onDestroy() {
super.onDestroy();
deleteCache(getApplicationContext());
}
이후에 deleteCache를 만들어 줍니다
여기서 File dir = context.getCacheDir(); 이 부분을 getFilesDir().getAbsolutePath();이렇게 바꿔보았으나
아예 디렉토리를 못찾더라구요
public static void deleteCache(Context context) {
try {
File dir = context.getCacheDir();
deleteDir(dir);
} catch (Exception e) {
e.printStackTrace();
}
}
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();
} else if(dir!= null && dir.isFile()) {
return dir.delete();
} else {
return false;
}
}
저 같은 상황에 직면하신 분들이 또 계실지는 모르겠으니
다운로드 파일의 path를 캐시폴더로 바꿔줍니다
<웹뷰에서 파일 다운로드는 이 게시물을 참고해주세요>
https://ssomethingnew.tistory.com/37
request.setDestinationInExternalFilesDir(getApplicationContext(),"/cache/", mFileName );
제가 찾은 답은 이렇게 작성해 주는 것이 었고요
원래는 "/cache/" 이 부분에 getCacheDir()을 넣었는데 getCacheDir()이거 자체에 컨텍스트 path까지 포함되어있어서
앞의 getApplicationContext() 부분의 path가 중복이 되더라고요
디렉토리 내부에 어떤파일이든 다 삭제하는 것이 목표였기 때문에 저는 전체 삭제 해줬습니다만
혹시 특정 파일명만 삭제하시고 싶으시다면 for문안에 파일명만 지정해주는 것도 좋을 것 같습니다.
'Java Android' 카테고리의 다른 글
[안드로이드]webview new tab/새 창 으로 열기 (0) | 2024.03.13 |
---|---|
[안드로이드]인코딩 디코딩 URL과 base64 (0) | 2023.11.03 |
[Java][Android]webview 파일 다운로드한 후 파일 열기 (0) | 2023.11.01 |
[Java][Android]webview에서 파일 업로드 (1) | 2023.10.31 |