일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- retrofi
- #안드로이드 개발자 #안드로이드 신입 #개발자 이직 #안드로이드 면접 #신입 개발자
- 안드로이드 아키텍쳐
- #리사이클러뷰
- #리사이클러뷰 어댑터
- 안드로이드 메모리릭
- 빗버킷 #bitbucket #authorization failed #깃
- 사용법
- 레트로핏
- 클린아키텍쳐
- 키스토어
- 안드로이드해상도
- retrofit2
- #SMS API #안드로이드 SMS #SMS Retriever
- 구글맵안돼요
- zeplin
- 메모리릭
- 리사이클러뷰 체크박스
- 안드로이드
- #android #안드로이드 #glide #gif #이미지다운로드
- #안드로이드
- MVVM
- retrofit
- 안드로이드 익명클래스
- 제플린
- #ContentProvider #App DataShare
Archives
- Today
- Total
땀이 삐질삐질 나는 개발 일기
Glide 4.x 버전 이상에서 Gif 및 이미지 다운받아 파일로 저장 본문
안녕하세요. 삐질삐질 개발하는 개발자 삐질입니다.
-
아래 로직들은 기본 전제조건으로 해당 퍼미션의 Grant가 선 조건입니다.
-
Internet , Read Storage , Write Strorage
-
또한 이 모든 작업은 BackgroundThread에서 실행되어야 합니다.
파일 다운로드
public void loadFile() {
try {
//글라이드를 통해 파일 다운로드
RequestManager requestManager = Glide.with(this);
File file = requestManager.downloadOnly().load(url).submit().get();
Log.e("파일사이즈", "" + file.length());
saveFile(file);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
파일 세이브
public void saveFile(File file) {
File localFile = new File(Environment.getExternalStoragePublicDirectory(filePath) .getPath());
if (!localFile.exists()) {// 디렉토리가 있는지 없는지 부터 체크
localFile.mkdirs(); //없으면 생성
}
String filepath = Environment.getExternalStoragePublicDirectory(filePath).getPath() + System.currentTimeMillis() + ".gif";
localFile = new File(filepath); // 만들고자 하는 파일패스 + 네임 객체 할당
try {
InputStream
is = new FileInputStream(file);
Log.d(TAG, "on do in background, url get input stream");
BufferedInputStream bis = new BufferedInputStream(is);
Log.d(TAG, "on do in background, create buffered input stream");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Log.d(TAG, "on do in background, create buffered array output stream");
byte[] img = new byte[1024];
int current = 0;
Log.d(TAG, "on do in background, write byte to baos");
while ((current = bis.read()) != -1) {
baos.write(current);
}
Log.d(TAG, "on do in background, done write");
Log.d(TAG, "on do in background, create fos");
FileOutputStream fos = new FileOutputStream(localFile);
fos.write(baos.toByteArray());
Log.d(TAG, "on do in background, write to fos");
fos.flush();
fos.close();
is.close();
Log.d(TAG, "on do in background, done write to fos");
scanMedia(); //미디어 스캔을 해줘야 , 시스템에서 바로 반영됨
} catch (Exception e) {
e.printStackTrace();
}
}
미디어 스캔
public void scanMedia(File file) {
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
}
안드로이드 초보 개발자를 위해 아래와 같은 카카오 오픈톡을 운영 중입니다.
https://open.kakao.com/o/gn4xqQ6open.kakao.com/o/gH0XvThcopen.kakao.com/o/gH0XvThc
'개발 Tip' 카테고리의 다른 글
BaseActivity 만들기 with DataBinding,ViewModel (Kotlin) (0) | 2020.01.06 |
---|---|
다중 프로젝트가 의존하는 Module Path를 각 개발 환경에서 자동화 하기 (0) | 2019.11.07 |
Android Alarm Manager 반복 설정 시 생길 수 있는 문제 (1) | 2019.08.01 |
RecyclerView Adapter에서 ViewHolder를 Nested Static Class 로 사용하는 이유. (0) | 2019.07.21 |
Retrofit2 기본 사용법 (4) | 2019.07.19 |
Comments