Skip to content

Commit

Permalink
profile images saved on cloud, can be accessed anywhere using Glide
Browse files Browse the repository at this point in the history
  • Loading branch information
hettysymes committed Jun 15, 2023
1 parent 761ad49 commit d15e065
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 35 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ configurations {
}

dependencies {
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'
implementation 'androidx.core:core-ktx:1.10.1' //7.0
Expand Down
8 changes: 2 additions & 6 deletions app/src/main/java/com/example/drp25/Database.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package com.example.drp25

import android.widget.LinearLayout
import android.widget.RatingBar
import com.example.drp25.matchers.MyMatcher
import com.example.drp25.matchers.RatingMatcherWithNationality
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.gson.Gson

private val unisRef = FirebaseDatabase.getInstance().reference.child("universities")
private val matcher: Matcher = MyMatcher()
Expand All @@ -20,8 +16,8 @@ fun sendStamp(uniId: String, userId: String, stampName: String) {
stampsRef.push().setValue(stampName)
}

fun updatePfp(uniId: String, userId: String, filepath: String) {
unisRef.child(uniId).child("users").child(userId).child("pfp").setValue(filepath)
fun updatePfp(uniId: String, userId: String, imgUri: String) {
unisRef.child(uniId).child("users").child(userId).child("pfp").setValue(imgUri)
}

fun deletePfp(uniId: String, userId: String) {
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/java/com/example/drp25/MatchActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.cardview.widget.CardView
import com.bumptech.glide.Glide
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError
import com.google.firebase.database.FirebaseDatabase
Expand Down Expand Up @@ -53,10 +54,11 @@ class MatchActivity : AppCompatActivity() {
val course = snapshot.child("course").value
val year = snapshot.child("year").value

val pfpPath = snapshot.child("pfp").value
if (pfpPath != null) {
val imageBitmap = BitmapFactory.decodeFile(pfpPath as String)
pfpImage.setImageBitmap(imageBitmap)
val pfpUri = snapshot.child("pfp").value
if (pfpUri != null) {
Glide.with(this@MatchActivity)
.load(pfpUri as String)
.into(pfpImage)
} else {
pfpImage.setImageResource(R.drawable.default_profile)
}
Expand Down
32 changes: 7 additions & 25 deletions app/src/main/java/com/example/drp25/UserProfileActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TableRow
import android.widget.TextView
import com.bumptech.glide.Glide
import com.example.drp25.databinding.ActivityUserProfileBinding
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError
Expand Down Expand Up @@ -51,8 +52,6 @@ class UserProfileActivity : AppCompatActivity() {
stampLayout = findViewById(R.id.stamp_layout)
noStampsTextView = findViewById(R.id.no_stamps_prompt)

imageFile = File(filesDir, "pfp$UNI_ID$USER_ID.png")

// set up views
val uniRef = FirebaseDatabase.getInstance().reference.child("universities")
.child(UNI_ID)
Expand All @@ -63,9 +62,11 @@ 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 pfpPath = snapshot.child("pfp").value
if (pfpPath != null) {
displayImageFromFile(pfpPath as String)
val pfpUri = snapshot.child("pfp").value
if (pfpUri != null) {
Glide.with(this@UserProfileActivity)
.load(pfpUri as String)
.into(binding.profileImageView)
} else {
binding.profileImageView.setImageResource(R.drawable.default_profile)
}
Expand Down Expand Up @@ -149,27 +150,8 @@ 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?) {
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, selectedImageUri.toString())
}
updatePfp(UNI_ID, USER_ID, imageFile.absolutePath)
displayImageFromFile(imageFile.absolutePath)
}

private fun displayImageFromFile(path: String) {
val imageBitmap = BitmapFactory.decodeFile(path)
binding.profileImageView.setImageBitmap(imageBitmap)
}
}

0 comments on commit d15e065

Please sign in to comment.