Upload an Image to Cloud Storage Kotlin
twenty
Cloud Storage
With Realtime Database and Deject Firestore you saved data to the database. Just what about files like photos, for example? While small pieces of information like posts, comments or users tend to exist just a few kilobytes of text, photos are much larger. Y'all don't desire to store photos in a database because it should be fast; storing and retrieving photos would extend both the startup fourth dimension and loading time when reading the database.
In this chapter, you'll learn how to store media files using another Firebase feature — Cloud Storage. You'll learn how to store an epitome to the cloud and how to become a URL to the paradigm to display it in your app.
Annotation: If you skipped previous capacity, you demand to setup Firebase to follow along. Do the following steps:
- Create a project in the Firebase panel.
- Enable Google sign-in.
- Set security rules to the exam fashion to allow everyone read and write access.
- Add google-service.json to both starter and last projects.
Note: To encounter how to exercise the steps in a higher place, become back to "Affiliate eleven: Firebase Overview" and "Chapter 12: Introduction to Firebase Realtime Database".
Be certain to use the starter project from this chapter past opening the cloud-storage folder and its starter project from the projects folder, rather than continuing with the concluding projection you lot previously worked on. This affiliate's starter project has a few things added to it, including placeholders for the code to add in this chapter.
Deject Storage overview
Cloud Storage is some other Firebase production used for saving files associated with your app. You can use information technology to shop large documents or media files like images or videos.
Since Cloud Storage operates with large files, it'south central to provide robust network connection mechanisms and fallbacks. Cloud Storage handles all of the potential network problems for yous. Depending on your connection, upload or download can take a while. If you lose the network connectedness in the eye of an upload or a download, the transfer will continue where it left off, after you reconnect to the network. This makes transferring your data very efficient.
Cloud Storage too has security features that will safely store your files away from the public. Y'all tin decide who can write data to and read data from the storage.
The foundations of Cloud Storage are the folders that you create to organize your information. You can so make up one's mind which users tin admission which folders.
There's more theory you could learn, but for now, you'll use the Firebase console to set up Cloud Storage and you'll use the WhatsUp app to store images and to download and display them to users on the app screen.
Getting started
Open your WhatsUp app in the Firebase console. Select Storage from the Develop bill of fare on the left. Yous'll see the post-obit screen:
Integrating Cloud Storage with your app
Open the starter project for this chapter then build and run the app. You lot'll run into an empty screen with a floating activity button in the bottom-right corner. When y'all click on the button, a File Explorer on your device will open up:
fun uploadPhoto(selectedImageUri: Uri, onSuccessAction: (Cord) -> Unit) { // 1 val photosReference = firebaseStorage.getReference(PHOTOS_REFERENCE) // 2 selectedImageUri.lastPathSegment?.let { segment -> // 3 val photoReference = photosReference.child(segment) // four photoReference.putFile(selectedImageUri) // five .continueWithTask(Continuation<UploadTask.TaskSnapshot, Task<Uri>> { task -> val exception = job.exception // 6 if (!job.isSuccessful && exception != null) { throw exception } return@Continuation photoReference.downloadUrl }) // seven .addOnCompleteListener { task -> // viii if (task.isSuccessful) { val downloadUri = task.result onSuccessAction(downloadUri.toString()) } } } } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if (requestCode == CHOOSE_IMAGE_REQUEST_CODE && resultCode == Activity.RESULT_OK) { val selectedImageUri = information?.data ?: return cloudStorageManager.uploadPhoto(selectedImageUri, ::onPhotoUploadSuccess) } }
Primal points
- Cloud Storage is a Firebase product used for saving files associated with your app.
- If y'all lose a network connection in the centre of the upload or a download, the transfer volition continue where it left off after you reconnect to the network.
- Deject Storage also has security features that will brand your files secure.
- The foundations of the Deject Storage are folders that you tin create to organize your data.
Where to go from here?
This chapter was just an introduction to Cloud Storage to show you how to store media files to the cloud. Yous learned how to ready Cloud Storage and how to upload and download files from it. Cloud Storage has many other features. To acquire more about them visit the official guidelines.
21. Conclusion xix. Securing Data in Cloud Firestore
gainesvearguat1972.blogspot.com
Source: https://www.raywenderlich.com/books/saving-data-on-android/v1.0/chapters/20-cloud-storage
0 Response to "Upload an Image to Cloud Storage Kotlin"
Postar um comentário