in storage/app/src/main/java/com/google/firebase/referencecode/storage/kotlin/StorageActivity.kt [125:271]
fun includesForUploadFiles() {
val storage = Firebase.storage
// [START upload_create_reference]
// Create a storage reference from our app
val storageRef = storage.reference
// Create a reference to "mountains.jpg"
val mountainsRef = storageRef.child("mountains.jpg")
// Create a reference to 'images/mountains.jpg'
val mountainImagesRef = storageRef.child("images/mountains.jpg")
// While the file names are the same, the references point to different files
mountainsRef.name == mountainImagesRef.name // true
mountainsRef.path == mountainImagesRef.path // false
// [END upload_create_reference]
val imageView = findViewById<ImageView>(R.id.imageView)
// [START upload_memory]
// Get the data from an ImageView as bytes
imageView.isDrawingCacheEnabled = true
imageView.buildDrawingCache()
val bitmap = (imageView.drawable as BitmapDrawable).bitmap
val baos = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos)
val data = baos.toByteArray()
var uploadTask = mountainsRef.putBytes(data)
uploadTask.addOnFailureListener {
// Handle unsuccessful uploads
}.addOnSuccessListener { taskSnapshot ->
// taskSnapshot.metadata contains file metadata such as size, content-type, etc.
// ...
}
// [END upload_memory]
// [START upload_stream]
val stream = FileInputStream(File("path/to/images/rivers.jpg"))
uploadTask = mountainsRef.putStream(stream)
uploadTask.addOnFailureListener {
// Handle unsuccessful uploads
}.addOnSuccessListener { taskSnapshot ->
// taskSnapshot.metadata contains file metadata such as size, content-type, etc.
// ...
}
// [END upload_stream]
// [START upload_file]
var file = Uri.fromFile(File("path/to/images/rivers.jpg"))
val riversRef = storageRef.child("images/${file.lastPathSegment}")
uploadTask = riversRef.putFile(file)
// Register observers to listen for when the download is done or if it fails
uploadTask.addOnFailureListener {
// Handle unsuccessful uploads
}.addOnSuccessListener { taskSnapshot ->
// taskSnapshot.metadata contains file metadata such as size, content-type, etc.
// ...
}
// [END upload_file]
// [START upload_with_metadata]
// Create file metadata including the content type
var metadata = storageMetadata {
contentType = "image/jpg"
}
// Upload the file and metadata
uploadTask = storageRef.child("images/mountains.jpg").putFile(file, metadata)
// [END upload_with_metadata]
// [START manage_uploads]
uploadTask = storageRef.child("images/mountains.jpg").putFile(file)
// Pause the upload
uploadTask.pause()
// Resume the upload
uploadTask.resume()
// Cancel the upload
uploadTask.cancel()
// [END manage_uploads]
// [START monitor_upload_progress]
// Observe state change events such as progress, pause, and resume
// You'll need to import com.google.firebase.storage.ktx.component1 and
// com.google.firebase.storage.ktx.component2
uploadTask.addOnProgressListener { (bytesTransferred, totalByteCount) ->
val progress = (100.0 * bytesTransferred) / totalByteCount
Log.d(TAG, "Upload is $progress% done")
}.addOnPausedListener {
Log.d(TAG, "Upload is paused")
}
// [END monitor_upload_progress]
// [START upload_complete_example]
// File or Blob
file = Uri.fromFile(File("path/to/mountains.jpg"))
// Create the file metadata
metadata = storageMetadata {
contentType = "image/jpeg"
}
// Upload file and metadata to the path 'images/mountains.jpg'
uploadTask = storageRef.child("images/${file.lastPathSegment}").putFile(file, metadata)
// Listen for state changes, errors, and completion of the upload.
// You'll need to import com.google.firebase.storage.ktx.component1 and
// com.google.firebase.storage.ktx.component2
uploadTask.addOnProgressListener { (bytesTransferred, totalByteCount) ->
val progress = (100.0 * bytesTransferred) / totalByteCount
Log.d(TAG, "Upload is $progress% done")
}.addOnPausedListener {
Log.d(TAG, "Upload is paused")
}.addOnFailureListener {
// Handle unsuccessful uploads
}.addOnSuccessListener {
// Handle successful uploads on complete
// ...
}
// [END upload_complete_example]
// [START upload_get_download_url]
val ref = storageRef.child("images/mountains.jpg")
uploadTask = ref.putFile(file)
val urlTask = uploadTask.continueWithTask { task ->
if (!task.isSuccessful) {
task.exception?.let {
throw it
}
}
ref.downloadUrl
}.addOnCompleteListener { task ->
if (task.isSuccessful) {
val downloadUri = task.result
} else {
// Handle failures
// ...
}
}
// [END upload_get_download_url]
}