본문 바로가기

android tech.

bitmap을 png 또는 jpg 파일로 저장

public void save(String path, String fileName, Bitmap target) {

File file = new File(path);

file.mkdirs();

String fileExtention = fileName.substring(fileName.lastIndexOf(".") + 1);

try {

String filePath = file.getPath() + "/" + fileName;

FileOutputStream fileOutput = new FileOutputStream(filePath);

BufferedOutputStream bufferOutput = new BufferedOutputStream(fileOutput);

if(fileExtention.equalsIgnoreCase("png")) {

target.compress(CompressFormat.PNG, 0, bufferOutput);

} else if (fileExtention.equalsIgnoreCase("jpg")) {

target.compress(CompressFormat.JPEG, 100, bufferOutput);

}

bufferOutput.flush();

bufferOutput.close();

fileOutput.close();

} catch(FileNotFoundException ex) {

Log.e(TAG, "file not found exception for saving file");

} catch(Exception e) {

Log.e(TAG, "save exception ::: " + e);

}

}


path - 저장할 경로

fileName - 저장할 파일이름, 확장자를 붙여준다. (확장자를 구분해서 png 또는 jpg 파일로 저장)

target - 저장할 bitmap

'android tech.' 카테고리의 다른 글

파일이 있는지 체크하기  (0) 2014.05.02
파일 복사해서 저장하기  (0) 2014.05.02
파일다운로드 및 저장  (0) 2014.05.02
layout에서 round rect 그리기  (0) 2014.05.02
레이아웃에서 include 사용시 주의 사항  (0) 2014.05.02