From 019118b3fdb2ec5521c8e2c6f2d86af0e0687840 Mon Sep 17 00:00:00 2001 From: hettysymes Date: Thu, 15 Jun 2023 22:02:57 +0100 Subject: [PATCH 1/6] can upload images to firebase storages but issues downloading & displaying --- app/build.gradle | 2 + .../main/java/com/example/drp25/Database.kt | 24 ++++++++++-- .../com/example/drp25/UserProfileActivity.kt | 39 +++++++++++++++++-- 3 files changed, 58 insertions(+), 7 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index c2d69aa..337dfaf 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -43,6 +43,8 @@ configurations { } dependencies { + implementation 'com.firebaseui:firebase-ui-storage:7.2.0' + implementation 'com.google.firebase:firebase-storage-ktx' implementation 'com.github.bumptech.glide:glide:4.12.0' implementation platform('com.google.firebase:firebase-bom:32.1.0') implementation 'com.google.code.gson:gson:2.8.8' diff --git a/app/src/main/java/com/example/drp25/Database.kt b/app/src/main/java/com/example/drp25/Database.kt index e40bf35..600a0e5 100644 --- a/app/src/main/java/com/example/drp25/Database.kt +++ b/app/src/main/java/com/example/drp25/Database.kt @@ -1,27 +1,44 @@ package com.example.drp25 +import android.net.Uri import com.example.drp25.matchers.MyMatcher import com.google.firebase.database.DataSnapshot import com.google.firebase.database.DatabaseError import com.google.firebase.database.FirebaseDatabase import com.google.firebase.database.ValueEventListener +import com.google.firebase.ktx.Firebase +import com.google.firebase.storage.ktx.storage +import java.io.File private val unisRef = FirebaseDatabase.getInstance().reference.child("universities") private val matcher: Matcher = MyMatcher() val matches = mutableSetOf() val matchObservers = mutableListOf() +val storage = Firebase.storage +val imageRef = storage.reference.child("images") + fun sendStamp(uniId: String, userId: String, stampName: String) { val stampsRef = unisRef.child(uniId).child("users").child(userId).child("stamps") stampsRef.push().setValue(stampName) } -fun updatePfp(uniId: String, userId: String, imgUri: String) { - unisRef.child(uniId).child("users").child(userId).child("pfp").setValue(imgUri) +fun updatePfp(uniId: String, userId: String, imgPath: String) { + //unisRef.child(uniId).child("users").child(userId).child("pfp").setValue(imgUri) + var file = Uri.fromFile(File(imgPath)) + val riversRef = imageRef.child("pfp_${uniId}_${userId}.png") + val uploadTask = riversRef.putFile(file) + + // Register observers to listen for when the download is done or if it fails + uploadTask.addOnFailureListener { + // Handle unsuccessful uploads + }.addOnSuccessListener { + unisRef.child(uniId).child("users").child(userId).child("pfp").setValue(true) + } } fun deletePfp(uniId: String, userId: String) { - unisRef.child(uniId).child("users").child(userId).child("pfp").removeValue() + unisRef.child(uniId).child("users").child(userId).child("pfp").setValue(false) } fun addMatchObserver(observer: Observer) { @@ -108,6 +125,7 @@ fun addUser(uniId: String, name: String, nationality: String, year: String, cour userRef.child("nationality").setValue(nationality) userRef.child("year").setValue(year) userRef.child("course").setValue(course) + userRef.child("pfp").setValue(false) } return userId } diff --git a/app/src/main/java/com/example/drp25/UserProfileActivity.kt b/app/src/main/java/com/example/drp25/UserProfileActivity.kt index 13d8b57..dc40642 100644 --- a/app/src/main/java/com/example/drp25/UserProfileActivity.kt +++ b/app/src/main/java/com/example/drp25/UserProfileActivity.kt @@ -20,6 +20,7 @@ import com.google.firebase.database.DataSnapshot import com.google.firebase.database.DatabaseError import com.google.firebase.database.FirebaseDatabase import com.google.firebase.database.ValueEventListener +import com.google.firebase.storage.FirebaseStorage import io.getstream.chat.android.client.models.UserId import java.io.File import java.io.FileOutputStream @@ -62,10 +63,10 @@ class UserProfileActivity : AppCompatActivity() { val name: String = snapshot.child("name").value as String val course = snapshot.child("course").value val year = snapshot.child("year").value - val pfpUri = snapshot.child("pfp").value - if (pfpUri != null) { + val hasPfp = snapshot.child("pfp").value as Boolean + if (hasPfp) { Glide.with(this@UserProfileActivity) - .load(pfpUri as String) + .load(imageRef.child("pfp_${UNI_ID}_${USER_ID}.png")) .into(binding.profileImageView) } else { binding.profileImageView.setImageResource(R.drawable.default_profile) @@ -150,7 +151,37 @@ class UserProfileActivity : AppCompatActivity() { super.onActivityResult(requestCode, resultCode, data) if (requestCode == PICK_IMAGE_REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null) { val selectedImageUri: Uri? = data.data - updatePfp(UNI_ID, USER_ID, selectedImageUri.toString()) + saveImageToFile(selectedImageUri) + } + } + + private fun saveImageToFile(imageUri: Uri?) { + val imageFile = File(filesDir, "temp.png") + imageUri?.let { uri -> + val inputStream = contentResolver.openInputStream(uri) + val outputStream = FileOutputStream(imageFile) + + inputStream?.use { input -> + outputStream.use { output -> + input.copyTo(output) + } + } + } + updatePfp(UNI_ID, USER_ID, imageFile.absolutePath) + Glide.with(this@UserProfileActivity) + .load(imageRef.child("pfp_${UNI_ID}_${USER_ID}.png")) + .into(binding.profileImageView) + } + + private fun getProfile() { + val pfpRef = imageRef.child("pfp_${UNI_ID}_${USER_ID}.png") + + val imageFile = File(filesDir, "temp.png") + + pfpRef.getFile(imageFile).addOnSuccessListener { + // Local temp file has been created + }.addOnFailureListener { + // Handle any errors } } From 105f5d403b1e09af6cc620d792d7ccba528b778e Mon Sep 17 00:00:00 2001 From: hettysymes Date: Thu, 15 Jun 2023 22:07:59 +0100 Subject: [PATCH 2/6] attempt with different display pfp method --- .../com/example/drp25/UserProfileActivity.kt | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/example/drp25/UserProfileActivity.kt b/app/src/main/java/com/example/drp25/UserProfileActivity.kt index dc40642..6387640 100644 --- a/app/src/main/java/com/example/drp25/UserProfileActivity.kt +++ b/app/src/main/java/com/example/drp25/UserProfileActivity.kt @@ -65,9 +65,7 @@ class UserProfileActivity : AppCompatActivity() { val year = snapshot.child("year").value val hasPfp = snapshot.child("pfp").value as Boolean if (hasPfp) { - Glide.with(this@UserProfileActivity) - .load(imageRef.child("pfp_${UNI_ID}_${USER_ID}.png")) - .into(binding.profileImageView) + displayPfp() } else { binding.profileImageView.setImageResource(R.drawable.default_profile) } @@ -168,21 +166,25 @@ class UserProfileActivity : AppCompatActivity() { } } updatePfp(UNI_ID, USER_ID, imageFile.absolutePath) - Glide.with(this@UserProfileActivity) - .load(imageRef.child("pfp_${UNI_ID}_${USER_ID}.png")) - .into(binding.profileImageView) } - private fun getProfile() { + private fun displayPfp() { val pfpRef = imageRef.child("pfp_${UNI_ID}_${USER_ID}.png") - val imageFile = File(filesDir, "temp.png") + val ONE_MEGABYTE: Long = 1024 * 1024 // Maximum size of the image in bytes - pfpRef.getFile(imageFile).addOnSuccessListener { - // Local temp file has been created - }.addOnFailureListener { - // Handle any errors - } + pfpRef.getBytes(ONE_MEGABYTE) + .addOnSuccessListener { imageData -> + // Convert the image data to a Bitmap + val bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.size) + + // Set the Bitmap to your ImageView + binding.profileImageView.setImageBitmap(bitmap) + } + .addOnFailureListener { exception -> + // Handle any errors that occurred while retrieving the image + // e.g., display a default image or show an error message + } } } \ No newline at end of file From 26cd619f4dd1afcb2a1e2571d13b5c7995b559eb Mon Sep 17 00:00:00 2001 From: hettysymes Date: Thu, 15 Jun 2023 22:16:31 +0100 Subject: [PATCH 3/6] image shows but is some lag --- .../com/example/drp25/UserProfileActivity.kt | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/example/drp25/UserProfileActivity.kt b/app/src/main/java/com/example/drp25/UserProfileActivity.kt index 6387640..3b9f5a5 100644 --- a/app/src/main/java/com/example/drp25/UserProfileActivity.kt +++ b/app/src/main/java/com/example/drp25/UserProfileActivity.kt @@ -169,22 +169,18 @@ class UserProfileActivity : AppCompatActivity() { } private fun displayPfp() { - val pfpRef = imageRef.child("pfp_${UNI_ID}_${USER_ID}.png") + val pfpRef = storage.getReferenceFromUrl("gs://drp25-d1bbb.appspot.com/images/pfp_imperialId_-NXPnWryIGR2S5aJmSGH.png") - val ONE_MEGABYTE: Long = 1024 * 1024 // Maximum size of the image in bytes + pfpRef.getBytes(Long.MAX_VALUE).addOnSuccessListener {imageData -> + // Use the bytes to display the image + // Convert the image data to a Bitmap + val bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.size) - pfpRef.getBytes(ONE_MEGABYTE) - .addOnSuccessListener { imageData -> - // Convert the image data to a Bitmap - val bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.size) - - // Set the Bitmap to your ImageView - binding.profileImageView.setImageBitmap(bitmap) - } - .addOnFailureListener { exception -> - // Handle any errors that occurred while retrieving the image - // e.g., display a default image or show an error message - } + // Set the Bitmap to your ImageView + binding.profileImageView.setImageBitmap(bitmap) + }.addOnFailureListener { + // Handle any errors + } } } \ No newline at end of file From 4b725148f45077b8514a5615860c4551a7517f01 Mon Sep 17 00:00:00 2001 From: hettysymes Date: Thu, 15 Jun 2023 22:21:27 +0100 Subject: [PATCH 4/6] pfp not saved locally at all --- .../main/java/com/example/drp25/Database.kt | 8 +++----- .../com/example/drp25/UserProfileActivity.kt | 20 +++---------------- 2 files changed, 6 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/example/drp25/Database.kt b/app/src/main/java/com/example/drp25/Database.kt index 600a0e5..b8d7c49 100644 --- a/app/src/main/java/com/example/drp25/Database.kt +++ b/app/src/main/java/com/example/drp25/Database.kt @@ -23,11 +23,9 @@ fun sendStamp(uniId: String, userId: String, stampName: String) { stampsRef.push().setValue(stampName) } -fun updatePfp(uniId: String, userId: String, imgPath: String) { - //unisRef.child(uniId).child("users").child(userId).child("pfp").setValue(imgUri) - var file = Uri.fromFile(File(imgPath)) - val riversRef = imageRef.child("pfp_${uniId}_${userId}.png") - val uploadTask = riversRef.putFile(file) +fun updatePfp(uniId: String, userId: String, imgUri: Uri) { + val pfpRef = imageRef.child("pfp_${uniId}_${userId}.png") + val uploadTask = pfpRef.putFile(imgUri) // Register observers to listen for when the download is done or if it fails uploadTask.addOnFailureListener { diff --git a/app/src/main/java/com/example/drp25/UserProfileActivity.kt b/app/src/main/java/com/example/drp25/UserProfileActivity.kt index 3b9f5a5..8cbaa36 100644 --- a/app/src/main/java/com/example/drp25/UserProfileActivity.kt +++ b/app/src/main/java/com/example/drp25/UserProfileActivity.kt @@ -149,33 +149,19 @@ class UserProfileActivity : AppCompatActivity() { super.onActivityResult(requestCode, resultCode, data) if (requestCode == PICK_IMAGE_REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null) { val selectedImageUri: Uri? = data.data - saveImageToFile(selectedImageUri) - } - } - - private fun saveImageToFile(imageUri: Uri?) { - val imageFile = File(filesDir, "temp.png") - imageUri?.let { uri -> - val inputStream = contentResolver.openInputStream(uri) - val outputStream = FileOutputStream(imageFile) - - inputStream?.use { input -> - outputStream.use { output -> - input.copyTo(output) - } + if (selectedImageUri != null) { + updatePfp(UNI_ID, USER_ID, selectedImageUri) } } - updatePfp(UNI_ID, USER_ID, imageFile.absolutePath) } private fun displayPfp() { - val pfpRef = storage.getReferenceFromUrl("gs://drp25-d1bbb.appspot.com/images/pfp_imperialId_-NXPnWryIGR2S5aJmSGH.png") + val pfpRef = imageRef.child("pfp_${UNI_ID}_${USER_ID}.png") pfpRef.getBytes(Long.MAX_VALUE).addOnSuccessListener {imageData -> // Use the bytes to display the image // Convert the image data to a Bitmap val bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.size) - // Set the Bitmap to your ImageView binding.profileImageView.setImageBitmap(bitmap) }.addOnFailureListener { From 4e8ba912f34a98251a95fada3ad7842b22a2661b Mon Sep 17 00:00:00 2001 From: hettysymes Date: Thu, 15 Jun 2023 22:28:19 +0100 Subject: [PATCH 5/6] pfp shown for matches from storage --- app/src/main/java/com/example/drp25/Database.kt | 16 ++++++++++++++++ .../main/java/com/example/drp25/MatchActivity.kt | 8 +++----- .../com/example/drp25/UserProfileActivity.kt | 16 +--------------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/example/drp25/Database.kt b/app/src/main/java/com/example/drp25/Database.kt index b8d7c49..f77700c 100644 --- a/app/src/main/java/com/example/drp25/Database.kt +++ b/app/src/main/java/com/example/drp25/Database.kt @@ -1,6 +1,8 @@ package com.example.drp25 +import android.graphics.BitmapFactory import android.net.Uri +import android.widget.ImageView import com.example.drp25.matchers.MyMatcher import com.google.firebase.database.DataSnapshot import com.google.firebase.database.DatabaseError @@ -18,6 +20,20 @@ val matchObservers = mutableListOf() val storage = Firebase.storage val imageRef = storage.reference.child("images") +fun displayPfp(uniId: String, userId: String, imageView: ImageView) { + val pfpRef = imageRef.child("pfp_${uniId}_${userId}.png") + + pfpRef.getBytes(Long.MAX_VALUE).addOnSuccessListener {imageData -> + // Use the bytes to display the image + // Convert the image data to a Bitmap + val bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.size) + // Set the Bitmap to your ImageView + imageView.setImageBitmap(bitmap) + }.addOnFailureListener { + // Handle any errors + } +} + fun sendStamp(uniId: String, userId: String, stampName: String) { val stampsRef = unisRef.child(uniId).child("users").child(userId).child("stamps") stampsRef.push().setValue(stampName) diff --git a/app/src/main/java/com/example/drp25/MatchActivity.kt b/app/src/main/java/com/example/drp25/MatchActivity.kt index 9f746e0..c3f8b8e 100644 --- a/app/src/main/java/com/example/drp25/MatchActivity.kt +++ b/app/src/main/java/com/example/drp25/MatchActivity.kt @@ -54,11 +54,9 @@ class MatchActivity : AppCompatActivity() { val course = snapshot.child("course").value val year = snapshot.child("year").value - val pfpUri = snapshot.child("pfp").value - if (pfpUri != null) { - Glide.with(this@MatchActivity) - .load(pfpUri as String) - .into(pfpImage) + val hasPfp = snapshot.child("pfp").value as Boolean + if (hasPfp) { + displayPfp(UNI_ID, matchId, pfpImage) } else { pfpImage.setImageResource(R.drawable.default_profile) } diff --git a/app/src/main/java/com/example/drp25/UserProfileActivity.kt b/app/src/main/java/com/example/drp25/UserProfileActivity.kt index 8cbaa36..8ebb6ba 100644 --- a/app/src/main/java/com/example/drp25/UserProfileActivity.kt +++ b/app/src/main/java/com/example/drp25/UserProfileActivity.kt @@ -65,7 +65,7 @@ class UserProfileActivity : AppCompatActivity() { val year = snapshot.child("year").value val hasPfp = snapshot.child("pfp").value as Boolean if (hasPfp) { - displayPfp() + displayPfp(UNI_ID, USER_ID, binding.profileImageView) } else { binding.profileImageView.setImageResource(R.drawable.default_profile) } @@ -155,18 +155,4 @@ class UserProfileActivity : AppCompatActivity() { } } - private fun displayPfp() { - val pfpRef = imageRef.child("pfp_${UNI_ID}_${USER_ID}.png") - - pfpRef.getBytes(Long.MAX_VALUE).addOnSuccessListener {imageData -> - // Use the bytes to display the image - // Convert the image data to a Bitmap - val bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.size) - // Set the Bitmap to your ImageView - binding.profileImageView.setImageBitmap(bitmap) - }.addOnFailureListener { - // Handle any errors - } - } - } \ No newline at end of file From 24a15ba278305a7900f3c86f4b5bea00a8f9bf36 Mon Sep 17 00:00:00 2001 From: hettysymes Date: Thu, 15 Jun 2023 22:29:04 +0100 Subject: [PATCH 6/6] removed unusued image file --- app/src/main/java/com/example/drp25/UserProfileActivity.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/main/java/com/example/drp25/UserProfileActivity.kt b/app/src/main/java/com/example/drp25/UserProfileActivity.kt index 8ebb6ba..f5fb5aa 100644 --- a/app/src/main/java/com/example/drp25/UserProfileActivity.kt +++ b/app/src/main/java/com/example/drp25/UserProfileActivity.kt @@ -32,7 +32,6 @@ class UserProfileActivity : AppCompatActivity() { private lateinit var binding: ActivityUserProfileBinding private lateinit var stampLayout: LinearLayout private lateinit var noStampsTextView: TextView - private lateinit var imageFile: File companion object { private const val PICK_IMAGE_REQUEST_CODE = 123