Saving Data on Android



Download 19,28 Mb.
Pdf ko'rish
bet289/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   284   285   286   287   288   289   290   291   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

CloudStorageManager.kt
 and replace 
uploadPhoto()
 with the following:
fun uploadPhoto(selectedImageUri: 
Uri
, onSuccessAction: (
String

-> 
Unit
) { 
  
// 1
 
  val photosReference = 
firebaseStorage.getReference(
PHOTOS_REFERENCE

 
  
// 2
 
  selectedImageUri.lastPathSegment?.
let
 { segment -> 
    
// 3
 
    val photoReference = photosReference.child(segment) 
 
    
// 4
 
    photoReference.putFile(selectedImageUri) 
        
// 5
 
        .continueWithTask(
Continuation
<
UploadTask
.
TaskSnapshot

Task
<
Uri
>> { task -> 
          val exception = task.exception 
            
// 6
 
          
if
 (!task.isSuccessful && exception != null) { 
            
throw
 exception 
          } 
          
return
@
Continuation
 photoReference.downloadUrl 
        }) 
        
// 7
 
        .addOnCompleteListener { task -> 
            
// 8
 
          
if
 (task.isSuccessful) { 
            val downloadUri = task.result 
            onSuccessAction(downloadUri.
toString
()) 
          } 
        } 
  } 
}
1. First, you get a reference to the 
photos
 folder that you created earlier, by calling 
getReference
 on 
firebaseStorage
. This is where you'll upload your photo.
2. You'll use 
lastPathSegment
 of the image URI as the name of the file that you're 
going to save.
3. Get a reference that points to the location to which you'll store the image.
4. To store the image to the reference, call 
putFile()
 on it and pass in the content 
URI of the image. This method stores the image asynchronously and returns an 
instance of 
UploadTask
 that you'll use to track the upload progress.
Saving Data on Android
Chapter 20: Cloud Storage
raywenderlich.com
285


5. Next, call 
continueWithTask()
 on 
UploadTask
 to get the download URL of the 
image you're uploading when the image upload finishes.
6. If the 
Task
 is successful, you return 
photoReference.downloadUrl
 which 
returns the 
Task
. Otherwise, you throw an exception.
7. Finally, attach an 
OnCompleteListener
 so you'll receive a notification when the 
upload finishes.
8. If the task is successful, you get the download URL by calling 
task.result
. You 
pass that result to 
onSuccessAction()
.
Now, open 

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   284   285   286   287   288   289   290   291   292




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish