Если вы работаете с kotlin, этот код основан на официальной документации и работает как брелок.
fun fileUpload() {
mFireBaseStorage = FirebaseStorage.getInstance()
mphotoStorageReference = mFireBaseStorage.getReference().child("alvaras")
//in case you want to compress your bitmap before upload
val bmp: Bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath) //filepath is the URI from the onActivityResult
val baos: ByteArrayOutputStream = ByteArrayOutputStream()
bmp.compress(Bitmap.CompressFormat.JPEG, 25, baos) //25 is the compression, cane be anything between 1 and 100, but 100 is no compression
//get the uri from the bitmap
val tempUri: Uri = getImageUri(this, bmp)
//transform the new compressed bmp in filepath uri
filePath = tempUri //update the filePath variable
var uploadTask = mphotoStorageReference.putFile(filePath)
val urlTask = uploadTask.continueWithTask(Continuation<UploadTask.TaskSnapshot, Task<Uri>> { task ->
if (!task.isSuccessful) {
task.exception?.let {
throw it
}
}
return@Continuation mphotoStorageReference.downloadUrl
}).addOnCompleteListener { task ->
if (task.isSuccessful) {
val downloadUri = task.result
urifinal = downloadUri.toString() //this is the url you want
val imageView: ImageView = findViewById(R.id.imageView1)
//show it in a imageview with glide with the url
Glide.with(this@MapsActivity).load(urifinal).into(imageView)
imageView.visibility = View.VISIBLE
} else {
// Handle failures
Toast.makeText(this, "An error occur", Toast.LENGTH_SHORT).show()
// ...
}
}
}