안드로이드에서 Bitmap을 Sdcard 같은 저장장치에 파일로 저장할 필요성이 있을 때가 있습니다.
간단한 함수로 파일로 저장이 가능합니다.
안드로이드 개발 - 비트맵을 파일로 변환하기(Bitmap to file)
1. 아래와 같은 함수를 등록해줍니다.
// Bitmap to File
public void SaveBitmapToFileCache(Bitmap bitmap, String strFilePath,
String filename) {
File file = new File(strFilePath);
// If no folders
if (!file.exists()) {
file.mkdirs();
// Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();
}
File fileCacheItem = new File(strFilePath + filename);
OutputStream out = null;
try {
fileCacheItem.createNewFile();
out = new FileOutputStream(fileCacheItem);
bitmap.compress(CompressFormat.JPEG, 100, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. 함수를 호출할 때에는 아래와 같이 호출합니다.(bitmap에는 비트맵, strFilePath에는 파일 경로, filename에는 파일 이름으로 사용할할 문자열을 지정해주면 됩니다.
SaveFileToFileCache(bitmap, strFilePath, filename);
도움이 되셨나요?
그럼 손가락을 눌러주세요:)
'Development > Android' 카테고리의 다른 글
| 안드로이드 개발 - ImageView 다양한 표시 방법 android:scaleType (0) | 2014.03.15 |
|---|---|
| 안드로이드 개발 - 파일(File) 유무 확인하기 (0) | 2014.03.11 |
| 안드로이드 개발 - 설치시 이클립스 Timeout 오류 해결하기 (0) | 2014.03.06 |
| 안드로이드 개발 - 연락처 선택/정보 가져오기(ACTION_PICK) (4) | 2013.10.04 |
| 안드로이드 개발 - 안드로이드 TextView 자동 링크걸기[Linkify, Autolink](인터넷, 이메일, 지도, 전화번호) (0) | 2013.06.14 |