-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddnewmentor.kt
110 lines (87 loc) · 3.77 KB
/
addnewmentor.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.muhammadahmad.i210790
import BaseActivity
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.Toast
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase
import com.muhammadahmad.i210790.databinding.ActivityAddnewmentorBinding
import java.io.ByteArrayOutputStream
import java.io.InputStream
import java.nio.ByteBuffer
import kotlin.io.encoding.Base64
class addnewmentor : BaseActivity() {
var sImage:String?= ""
private lateinit var db:DatabaseReference
private lateinit var binding:ActivityAddnewmentorBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityAddnewmentorBinding.inflate(layoutInflater)
setContentView(binding.root)
// Initialize Firebase instances
}
fun backtomainmenu(view: View)
{
val forgot = Intent(this,mainmenu::class.java)
startActivity(forgot)
// finish()
}
fun upload(view: View) {
// Get user input
var email = binding.addnewmentorEmail.text.toString()
var description = binding.addnewmentorDescription.text.toString()
var status = binding.addnewmentorStatus.text.toString()
if (email.isEmpty() || description.isEmpty() || status.isEmpty() || sImage.isNullOrEmpty()) {
Toast.makeText(this, "Please fill in all fields", Toast.LENGTH_SHORT).show()
return
}
db = FirebaseDatabase.getInstance().getReference("mentors")
val item = datamentor(email, description, status, sImage)
val databaseReference = FirebaseDatabase.getInstance().reference
val id = databaseReference.push().key
db.child(id.toString()).setValue(item).addOnSuccessListener {
binding.addnewmentorEmail.text.clear()
binding.addnewmentorDescription.text.clear()
binding.addnewmentorStatus.text.clear()
sImage = ""
Toast.makeText(this, "Mentor Added Successfully",Toast.LENGTH_SHORT).show()
}
.addOnFailureListener{
Toast.makeText(this, "Data Not Inserted",Toast.LENGTH_SHORT).show()
}
}
fun make_photo(view: View) {
var myfileintent = Intent(Intent.ACTION_GET_CONTENT)
myfileintent.setType("image/*")
ActivityResultLauncher.launch(myfileintent)
}
fun make_video(view: View) {
// Handle opening video recording functionality if needed
}
private val ActivityResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
val uri = result.data?.data
try {
val inputStream = contentResolver.openInputStream(uri!!)
val myBitmap = BitmapFactory.decodeStream(inputStream)
val stream = ByteArrayOutputStream()
myBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)
val bytes = stream.toByteArray()
sImage = android.util.Base64.encodeToString(bytes, android.util.Base64.DEFAULT)
binding.btnUploadVideo.setImageBitmap(myBitmap)
inputStream?.close()
Toast.makeText(this, "Image Selected", Toast.LENGTH_SHORT).show()
} catch (ex: Exception) {
Toast.makeText(this, ex.message.toString(), Toast.LENGTH_SHORT).show()
}
}
}
}