I am trying to make an application that captures an image and saves it to Firebase storage.
this is the code that is responsible for uploading the photo
private fun uploadAbsensi(bitmap: Bitmap, currentDate: String, currentTime: String) {
val userId = "testID" //should have no relevance to the issue
val absensiRef = db.collection("absensi")
.document(userId)
.collection("harian")
.document(currentDate)
val storage = FirebaseStorage.getInstance()
absensiRef.get().addOnSuccessListener { documentSnapshot : DocumentSnapshot ->
if (documentSnapshot.exists()) {
Toast.makeText(mainActivity, "Anda sudah melakukan absensi hari ini", Toast.LENGTH_SHORT).show()
} else {
// Upload photo to Firebase Storage
val storageRef = storage.reference.child("absensi/${userId}/${currentDate}.jpg")
val baos = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos)
val data = baos.toByteArray()
val uploadTask = storageRef.putBytes(data)
uploadTask.addOnSuccessListener {
storageRef.downloadUrl.addOnSuccessListener { uri ->
// Save attendance data to Firestore
val absensiData = mapOf(
"userId" to userId,
"photoUrl" to uri.toString(),
"date" to currentDate,
"time" to currentTime
)
absensiRef.set(absensiData).addOnSuccessListener {
Toast.makeText(mainActivity, "Absensi berhasil disimpan", Toast.LENGTH_SHORT).show()
// finish() // Return after successful attendance
}
}
}.addOnFailureListener {
Toast.makeText(mainActivity, "Gagal mengunggah foto", Toast.LENGTH_SHORT).show()
}
}
}
}
this is my build.gradle (app-level)
plugins {
// alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
id("com.android.application")
id("com.google.gms.google-services")
}
android {
namespace = "com.example.if570_f_000000068544_kevinwijayahadinata_uts"
compileSdk = 34
defaultConfig {
applicationId = "com.example.if570_f_000000068544_kevinwijayahadinata_uts"
minSdk = 24
targetSdk = 34
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
viewBinding = true
}
}
dependencies {
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
implementation(libs.androidx.activity)
implementation(libs.androidx.constraintlayout)
implementation(libs.androidx.annotation)
implementation(libs.androidx.lifecycle.livedata.ktx)
implementation(libs.androidx.lifecycle.viewmodel.ktx)
implementation(libs.firebase.firestore.ktx)
implementation(libs.firebase.auth.ktx)
implementation(libs.androidx.legacy.support.v4)
implementation(libs.androidx.recyclerview)
implementation(libs.firebase.storage.ktx)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
implementation(platform("com.google.firebase:firebase-bom:33.4.0"))
implementation("com.google.firebase:firebase-analytics")
implementation ("com.google.firebase:firebase-firestore-ktx:24.7.1")
implementation ("com.google.firebase:firebase-storage-ktx:19.3.1")
implementation ("androidx.appcompat:appcompat:1.6.1")
implementation ("androidx.navigation:navigation-fragment-ktx:2.5.3")
implementation ("androidx.navigation:navigation-ui-ktx:2.5.3")
implementation ("com.google.android.material:material:1.9.0")
implementation("com.github.bumptech.glide:glide:4.14.2")
}
and my project-level build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id("com.google.gms.google-services") version "4.4.2" apply false
alias(libs.plugins.android.application) apply false
alias(libs.plugins.kotlin.android) apply false
}
however, when i try to upload the image to the storage I get this error
Failed to get service from broker. (Ask Gemini)
java.lang.SecurityException: Unknown calling package name 'com.google.android.gms'.
at android.os.Parcel.createExceptionOrNull(Parcel.java:2390)
at android.os.Parcel.createException(Parcel.java:2374)
at android.os.Parcel.readException(Parcel.java:2357)
at android.os.Parcel.readException(Parcel.java:2299)
I have imported every relevant library in the fragment where my uploadAbsensi() function lies (given that there are no errors in the file), and in my firebase I have set the rules to be able to read and write data. I have never created an android app before, so please forgive my lack of knowledge if the solution is simple.
You need to sign in to view this answers