-
|Android| |Java| 파일 용량 줄이기프로그래밍/Java 2020. 8. 25. 18:05
1.
서버에 이미지파일을 업로드 해야하는 작업이 생겼다.
해당 이미지파일은 증빙용도 이미지파일이기에.
작은 용량으로 업로드해야해서 용량을 줄이는 작업이 필요했다.
2.
private String getImageViewToFileResizing(Bitmap bitmap){ String mUri = ""; if(bitmap != null) { FileOutputStream fout = null; try { int MAX_IMAGE_SIZE = 150 * 1024; // max final file size int compressQuality = 100; // quality decreasing by 5 every loop. (start from 99) int streamLength = MAX_IMAGE_SIZE; album_fileName = System.currentTimeMillis() + "_pic.jpg"; File file = new File(dirFile,album_fileName); while(streamLength >= MAX_IMAGE_SIZE){ fout = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG, compressQuality, fout); streamLength = (int) file.length(); compressQuality -= 5; } fout.flush(); mUri = file.getPath(); } catch (Exception e) { e.printStackTrace(); } finally{ if(fout!=null){ try{ fout.close(); }catch(Exception e){ } }else{ } } } return mUri; }
Bitmap 이미지파일을 압축시켜 로컬저장소에 저장하는로직이며.
해당 파일의 크기를 측정해 사이즈가크면 파일의 품질을 줄여나가서
원하는 사이즈의 파일을 만드는 로직이다.
해당 소스의경우 150kb 이하로 설정했으며, 원하는 사이즈로 설정해서
작업하면된다.
이후. 해당파일을 서버에 업로드하는 작업으로 개발 완료됐다.
3.
끗
'프로그래밍 > Java' 카테고리의 다른 글
|Android| |Java| Custom Checkbox 만들기 (0) 2023.08.17 |Android| |Java| 이미지 사이즈 줄이기 (0) 2020.09.16 |Android| |Java| Room 사용하기 (0) 2020.06.30 |Android| |Java| 문자열 분할하여 배열에 담기. (0) 2020.06.15 |Android| |Java| 로컬저장소에 로그 Text 남기기. (0) 2020.06.11