Image를 처리하기위해 임시파일로 cache하는 법
byteArray
public static byte[] saveBitmapToJpeg(Bitmap bitmap) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); // 넘거 받은 bitmap을 jpeg(손실압축)으로 저장해줌
return out.toByteArray(); // 임시파일 저장경로를 리턴해주면 끝!
}
File path
public static String saveBitmapToJpeg(Context context, Bitmap bitmap, String name) {
File storage = context.getCacheDir(); // 이 부분이 임시파일 저장 경로
String fileName = name + ".jpg"; // 파일이름은 마음대로!
File tempFile = new File(storage, fileName);
try {
tempFile.createNewFile(); // 파일을 생성해주고
FileOutputStream out = new FileOutputStream(tempFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); // 넘거 받은 bitmap을 jpeg(손실압축)으로 저장해줌
out.close(); // 마무리로 닫아줍니다.
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return tempFile.getAbsolutePath(); // 임시파일 저장경로를 리턴해주면 끝!
}