android tech.

bitmap을 png 또는 jpg 파일로 저장

zimamdero 2014. 5. 2. 15:05

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