본문 바로가기

프로그래밍/안드로이드

파이어베이스 안드로이드 사진 다운받기 getFile, FileOutputStream, BitmapFactory

728x90
반응형

파이어베이스 안드로이드 사진 다운받기 getFile, FileOutputStream, BitmapFactory

 

파일이 있는지 확인하고 없으면 파일 생성

 

다운로드한 파일은 비트맵 jpeg 압축 후 저장

 

/* 파일이 있는지 확인 */
File file = new File(getFilesDir(), "UserProfile");
if(!file.isFile()) {
    /* 파이어베이스 서버에서 프로필 이미지를 다운받음 */
    try {
        /* 임시파일 생성 */
        File tempFile = File.createTempFile("images", "jpeg");
        StorageReference sgRef = FirebaseStorage.getInstance().getReference();
        sgRef.child("UserAccount")
                .child("Email")
                .child(userAccount.getUid())
                .child("ProfileImage").getFile(tempFile).addOnSuccessListener(taskSnapshot -> {

                    try{
                        FileOutputStream fos = openFileOutput("UserProfile", Context.MODE_PRIVATE);
                        Bitmap bitmap = BitmapFactory.decodeFile(tempFile.getPath());
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });

    } catch (IOException e) {
        e.printStackTrace();
    }
}

 

728x90
반응형