DEV Community

loading...

Kotlin Firebase Storage – Upload Data from Memory, Local File, Stream | Android

loizenai
Software Engineer - Founder at https://loizenai.com
・2 min read

https://grokonez.com/android/kotlin-firebase-storage-upload-file-data-memory-local-file-stream-android

Kotlin Firebase Storage – Upload Data from Memory, Local File, Stream | Android

Firebase Cloud Storage helps us upload and share rich content data. Data is stored in a Google Cloud Storage bucket. With Firebase, we can perform robust operations (download/upload) regardless of network quality with strong security (Cloud Storage integrates with Firebase Authentication) and high scalability.

In this tutorial, we're gonna look at ways to upload data from Memory, Local file, Stream in an Android App using Firebase Storage with Kotlin.

Related Posts:

I. How to upload file

To use the Firebase Storage to upload data, we need:

  • add Firebase to Android App & enable Firebase Auth
  • create a reference to the full path of the file, including the file name
  • upload data using putBytes() for in-memory data, putStream() for stream data, putFile() for local file.

    0. Add Firebase to Android App

    0.1 Add Firebase Storage

    Steps to import and enable Firebase Storage is just like steps for Firebase Auth. Just follow: Add_Firebase_to_Android_Project

    0.2 Add Firebase Auth

    By default, only authenticated users can upload or download data, so we need Firebase Authentication for next step. Go to Your Firebase Project Console -> Authentication -> SIGN-IN METHOD -> Enable Email/Password.

To do without setting up Authentication, we can change the rules in the Firebase Console -> choose Project -> Storage section on the left -> Rules tab:


// change the code below
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth != null;
    }
  }
}

0.3 Check

After adding Firebase Auth & Realtime DB, we can see:

  • build.gradle (project-level):

https://grokonez.com/android/kotlin-firebase-storage-upload-file-data-memory-local-file-stream-android

Kotlin Firebase Storage – Upload Data from Memory, Local File, Stream | Android

Discussion (0)