Code
private void bitmapToPNG(Bitmap bitmap, ChatMstVO chatMstVO, Boolean oneToOne){
try {
shortcutDrawBitmap = bitmap;
String fileName = chatMstVO.getChatRoomKey() + ".png";
String cashPath = view.getContext().getCacheDir().getAbsolutePath() + "//shortcut";
File storage = new File(cashPath);
if(!storage.exists()){
storage.mkdirs();
}
File tempFile = new File(storage, fileName);
tempFile.createNewFile();
FileOutputStream out = new FileOutputStream(tempFile);
if(oneToOne){
getCroppedBitmap(shortcutDrawBitmap).compress(Bitmap.CompressFormat.PNG, 100, out);
}else{
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
}
out.close();
} catch (FileNotFoundException e){
Log.e(TAG,"FileNotFoundException : " + e.getMessage());
} catch (IOException e) {
Log.e(TAG,"IOException : " + e.getMessage());
}
}
Bitmap을 가져와 png파일로 해당 앱의 캐시 폴더에 저장해준다. chatMstVO는 데이터 클래스인데 그곳에서 해당 클래스데이터의 roomKey라는 고유값을 가져와 파일 이름으로 지정해준다. 파일 저장시 파일 이름이 같다면 덮어씌우기가 되기 때문에 다른 다른 파일을 같은 파일로 착각하는 경우를 방지 하기 위해 고유값을 사용한다. cashPath에 저장되게 되는데 저장경로를 변경하려면 cashPath 경로를 바꾸면 된다.
해당 파일을 저장하여 Shortcuts 생성시 아이콘으로 사용하였으며 다음 페이지에서는 저장한 캐시파일을 삭제하는 법을 다루어 보려고 한다.
Reference
'Android > Android Java' 카테고리의 다른 글
[Android Java] File.mkdir()과 File.mkdirs()의 차이 (0) | 2023.11.03 |
---|---|
[Android Java] File 삭제, Cache 삭제 (1) | 2023.11.03 |
[Android Java] Bitmap 이미지 원형으로 자르기 (0) | 2023.11.02 |
[Android Java] Bitmap을 Drawable로, Drawable을 Bitmap으로 변환 (2) | 2023.11.02 |
[Android Java] Bitmap, Canvas, Paint, Drawable 이란? (0) | 2023.11.02 |