How to get Bitmap from an Uri?

前端 未结 16 1381
时光说笑
时光说笑 2020-11-22 10:41

How to get a Bitmap object from an Uri (if I succeed to store it in /data/data/MYFOLDER/myimage.png or file///data/data/MYFOLDER/myimage.png) to u

16条回答
  •  甜味超标
    2020-11-22 10:56

    I have try a lot of ways. this work for me perfectly.

    If you choose pictrue from Gallery. You need to be ware of getting Uri from intent.clipdata or intent.data, because one of them may be null in different version.

      private fun onChoosePicture(data: Intent?):Bitmap {
            data?.let {
                var fileUri:Uri? = null
    
                  data.clipData?.let {clip->
                      if(clip.itemCount>0){
                          fileUri = clip.getItemAt(0).uri
                      }
                  }
                it.data?.let {uri->
                    fileUri = uri
                }
    
    
                   return MediaStore.Images.Media.getBitmap(this.contentResolver, fileUri )
    }
    

提交回复
热议问题