Android download bitmap to file Using Glide   (Android Gilde을 이용한 Bitmap 파일 변환)공부방/Android2020. 3. 4. 13:34
      Table of Contents
        
 Glide.with(applicationContext)
                .asBitmap().load(url)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
                .listener(object : RequestListener<Bitmap> {
                    override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Bitmap>?, isFirstResource: Boolean): Boolean {
                        return false
                    }
                    override fun onResourceReady(
                        resource: Bitmap?,
                        model: Any?,
                        target: Target<Bitmap>?,
                        dataSource: DataSource?,
                        isFirstResource: Boolean
                    ): Boolean {
                        try {
                            val uuid = UUID.randomUUID().toString().replace("-", "") 
                            //랜덤 UUID 생성
                            val fileName = "${uuid}.jpg"
                            val file = File(applicationContext.cacheDir, fileName)
                            //임시 디렉토리에 파일 생성
                            file.createNewFile()
                            // 파일을 쓸 수 있는 스트림을 준비.
                            val out = FileOutputStream(file)
                            // compress 함수를 사용해 스트림에 비트맵을 저장.
                            resource?.compress(Bitmap.CompressFormat.JPEG, 100, out)
                            out.close()
                            // 스트림 사용후 닫아줌.
                            
                            
                            //DO something
                            
                            
                        } catch (e : FileNotFoundException) {
                            Log.e("MYTAG", "FileNotFoundException : " + e.message)
                        } catch (e: IOException) {
                            Log.e("MYTAG","IOException : " + e.message)
                        }
                        return false
                    }
                }).submit()반응형
    
    
    
  '공부방 > Android' 카테고리의 다른 글
@soycrab :: 꿀맛코딩
            행복한 코딩을 위하여!
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!
 
                  