-
Notifications
You must be signed in to change notification settings - Fork 403
/
StorageActivity.kt
504 lines (413 loc) · 17.3 KB
/
StorageActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package com.google.firebase.referencecode.storage.kotlin
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.tasks.OnFailureListener
import com.google.firebase.Firebase
import com.google.firebase.initialize
import com.google.firebase.referencecode.storage.R
import com.google.firebase.storage.FirebaseStorage
import com.google.firebase.storage.StorageException
import com.google.firebase.storage.StorageReference
import com.google.firebase.storage.component1
import com.google.firebase.storage.component2
import com.google.firebase.storage.component3
import com.google.firebase.storage.storage
import com.google.firebase.storage.storageMetadata
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileInputStream
abstract class StorageActivity : AppCompatActivity() {
// [START storage_field_declaration]
lateinit var storage: FirebaseStorage
// [END storage_field_declaration]
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_storage)
// [START storage_field_initialization]
storage = Firebase.storage
// [END storage_field_initialization]
includesForCreateReference()
}
private fun includesForCreateReference() {
val storage = Firebase.storage
// ## Create a Reference
// [START create_storage_reference]
// Create a storage reference from our app
var storageRef = storage.reference
// [END create_storage_reference]
// [START create_child_reference]
// Create a child reference
// imagesRef now points to "images"
var imagesRef: StorageReference? = storageRef.child("images")
// Child references can also take paths
// spaceRef now points to "images/space.jpg
// imagesRef still points to "images"
var spaceRef = storageRef.child("images/space.jpg")
// [END create_child_reference]
// ## Navigate with References
// [START navigate_references]
// parent allows us to move our reference to a parent node
// imagesRef now points to 'images'
imagesRef = spaceRef.parent
// root allows us to move all the way back to the top of our bucket
// rootRef now points to the root
val rootRef = spaceRef.root
// [END navigate_references]
// [START chain_navigation]
// References can be chained together multiple times
// earthRef points to 'images/earth.jpg'
val earthRef = spaceRef.parent?.child("earth.jpg")
// nullRef is null, since the parent of root is null
val nullRef = spaceRef.root.parent
// [END chain_navigation]
// ## Reference Properties
// [START reference_properties]
// Reference's path is: "images/space.jpg"
// This is analogous to a file path on disk
spaceRef.path
// Reference's name is the last segment of the full path: "space.jpg"
// This is analogous to the file name
spaceRef.name
// Reference's bucket is the name of the storage bucket that the files are stored in
spaceRef.bucket
// [END reference_properties]
// ## Full Example
// [START reference_full_example]
// Points to the root reference
storageRef = storage.reference
// Points to "images"
imagesRef = storageRef.child("images")
// Points to "images/space.jpg"
// Note that you can use variables to create child values
val fileName = "space.jpg"
spaceRef = imagesRef.child(fileName)
// File path is "images/space.jpg"
val path = spaceRef.path
// File name is "space.jpg"
val name = spaceRef.name
// Points to "images"
imagesRef = spaceRef.parent
// [END reference_full_example]
}
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.component1 and
// com.google.firebase.storage.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.component1 and
// com.google.firebase.storage.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]
}
fun includesForDownloadFiles() {
val storage = Firebase.storage
// [START download_create_reference]
// Create a storage reference from our app
val storageRef = storage.reference
// Create a reference with an initial file path and name
val pathReference = storageRef.child("images/stars.jpg")
// Create a reference to a file from a Google Cloud Storage URI
val gsReference = storage.getReferenceFromUrl("gs://bucket/images/stars.jpg")
// Create a reference from an HTTPS URL
// Note that in the URL, characters are URL escaped!
val httpsReference = storage.getReferenceFromUrl(
"https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg",
)
// [END download_create_reference]
// [START download_to_memory]
var islandRef = storageRef.child("images/island.jpg")
val ONE_MEGABYTE: Long = 1024 * 1024
islandRef.getBytes(ONE_MEGABYTE).addOnSuccessListener {
// Data for "images/island.jpg" is returned, use this as needed
}.addOnFailureListener {
// Handle any errors
}
// [END download_to_memory]
// [START download_to_local_file]
islandRef = storageRef.child("images/island.jpg")
val localFile = File.createTempFile("images", "jpg")
islandRef.getFile(localFile).addOnSuccessListener {
// Local temp file has been created
}.addOnFailureListener {
// Handle any errors
}
// [END download_to_local_file]
// [START download_via_url]
storageRef.child("users/me/profile.png").downloadUrl.addOnSuccessListener {
// Got the download URL for 'users/me/profile.png'
}.addOnFailureListener {
// Handle any errors
}
// [END download_via_url]
// [START download_full_example]
storageRef.child("users/me/profile.png").getBytes(Long.MAX_VALUE).addOnSuccessListener {
// Use the bytes to display the image
}.addOnFailureListener {
// Handle any errors
}
// [END download_full_example]
}
fun includesForFileMetadata() {
val storage = Firebase.storage
// [START metadata_get_storage_reference]
// Create a storage reference from our app
val storageRef = storage.reference
// Get reference to the file
val forestRef = storageRef.child("images/forest.jpg")
// [END metadata_get_storage_reference]
// [START get_file_metadata]
forestRef.metadata.addOnSuccessListener { metadata ->
// Metadata now contains the metadata for 'images/forest.jpg'
}.addOnFailureListener {
// Uh-oh, an error occurred!
}
// [END get_file_metadata]
// [START update_file_metadata]
// Create file metadata including the content type
val metadata = storageMetadata {
contentType = "image/jpg"
setCustomMetadata("myCustomProperty", "myValue")
}
// Update metadata properties
forestRef.updateMetadata(metadata).addOnSuccessListener { updatedMetadata ->
// Updated metadata is in updatedMetadata
}.addOnFailureListener {
// Uh-oh, an error occurred!
}
// [END update_file_metadata]
}
fun includesForMetadata_delete() {
val storage = Firebase.storage
val storageRef = storage.reference
val forestRef = storageRef.child("images/forest.jpg")
// [START delete_file_metadata]
// Create file metadata with property to delete
val metadata = storageMetadata {
contentType = null
}
// Delete the metadata property
forestRef.updateMetadata(metadata).addOnSuccessListener { updatedMetadata ->
// updatedMetadata.contentType should be null
}.addOnFailureListener {
// Uh-oh, an error occurred!
}
// [END delete_file_metadata]
}
fun includesForMetadata_custom() {
// [START custom_metadata]
val metadata = storageMetadata {
setCustomMetadata("location", "Yosemite, CA, USA")
setCustomMetadata("activity", "Hiking")
}
// [END custom_metadata]
}
fun includesForDeleteFiles() {
val storage = Firebase.storage
// [START delete_file]
// Create a storage reference from our app
val storageRef = storage.reference
// Create a reference to the file to delete
val desertRef = storageRef.child("images/desert.jpg")
// Delete the file
desertRef.delete().addOnSuccessListener {
// File deleted successfully
}.addOnFailureListener {
// Uh-oh, an error occurred!
}
// [END delete_file]
}
fun nonDefaultBucket() {
// [START storage_non_default_bucket]
// Get a non-default Storage bucket
val storage = Firebase.storage("gs://my-custom-bucket")
// [END storage_non_default_bucket]
}
fun customApp() {
val customApp = Firebase.initialize(this)
// [START storage_custom_app]
// Get the default bucket from a custom FirebaseApp
val storage = Firebase.storage(customApp!!)
// Get a non-default bucket from a custom FirebaseApp
val customStorage = Firebase.storage(customApp, "gs://my-custom-bucket")
// [END storage_custom_app]
}
fun listAllFiles() {
// [START storage_list_all]
val storage = Firebase.storage
val listRef = storage.reference.child("files/uid")
// You'll need to import com.google.firebase.storage.component1 and
// com.google.firebase.storage.component2
listRef.listAll()
.addOnSuccessListener { (items, prefixes) ->
for (prefix in prefixes) {
// All the prefixes under listRef.
// You may call listAll() recursively on them.
}
for (item in items) {
// All the items under listRef.
}
}
.addOnFailureListener {
// Uh-oh, an error occurred!
}
// [END storage_list_all]
}
private fun processResults(items: List<StorageReference>, prefixes: List<StorageReference>) {
}
// [START storage_list_paginated]
fun listAllPaginated(pageToken: String?) {
val storage = Firebase.storage
val listRef = storage.reference.child("files/uid")
// Fetch the next page of results, using the pageToken if we have one.
val listPageTask = if (pageToken != null) {
listRef.list(100, pageToken)
} else {
listRef.list(100)
}
// You'll need to import com.google.firebase.storage.component1 and
// com.google.firebase.storage.component2
listPageTask
.addOnSuccessListener { (items, prefixes, pageToken) ->
// Process page of results
processResults(items, prefixes)
// Recurse onto next page
pageToken?.let {
listAllPaginated(it)
}
}.addOnFailureListener {
// Uh-oh, an error occurred.
}
}
// [END storage_list_paginated]
// [START storage_custom_failure_listener]
internal inner class MyFailureListener : OnFailureListener {
override fun onFailure(exception: Exception) {
val errorCode = (exception as StorageException).errorCode
val errorMessage = exception.message
// test the errorCode and errorMessage, and handle accordingly
}
}
// [END storage_custom_failure_listener]
companion object {
const val TAG = "kotlin.StorageActivity"
}
}