好,我有两个活动,在主要活动中,有一个图像按钮,可将您带到个人资料页面活动,您可以单击另一个图像按钮并访问您的照片库以设置个人资料图片,然后将其保存到Firebase存储中,并作为数据库中的url。所有这些都有效,问题在于完成所有操作并返回到主要活动(我将覆盖后退按钮并开始新的主要活动)后,尽管Log.d检查报告,图像按钮仍显示先前的配置文件图像我正在访问更新的URL。甚至更陌生,如果我再次进入个人资料活动,它将显示更新的个人资料图片,当我再次回到主要活动时,新图像现在将正确显示。
MainActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
binding.activity = this
setSupportActionBar(toolbar)
val emailer=intent.getStringExtra("passemail")
val toggle = ActionBarDrawerToggle(
this, drawer_layout, toolbar, R.string.main_drawer_open, R.string.main_drawer_close)
drawer_layout.addDrawerListener(toggle)
toggle.syncState()
val profile = findViewById<de.hdodenhof.circleimageview.CircleImageView>(R.id.profilebutton)
val ref = FirebaseDatabase.getInstance().getReference("/users/$emailer")
val valueEventListener = object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
val user = dataSnapshot.getValue(User::class.java)
Log.d("msg0 main", "image url: " + user!!.profile_pic);
if(user!!.profile_pic.length > 0){
Picasso.get().load(user!!.profile_pic).into(profile)
}
}
override fun onCancelled(databaseError: DatabaseError) {
Log.d("msg0", databaseError.message) //Don't ignore errors!
}
}
ref.addListenerForSingleValueEvent(valueEventListener)
profile.setOnClickListener {
val intent = Intent(this, ProfilePage::class.java)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK.or(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.putExtra("passemail",emailer)
startActivity(intent)
}
}
}
ProfileActivity
class ProfilePage : AppCompatActivity() {
private lateinit var auth: FirebaseAuth
private lateinit var profilepic: de.hdodenhof.circleimageview.CircleImageView
private lateinit var profileURi: Uri
private lateinit var emailz: String
private fun saveUserToDatabase(profilepicURL: String, emailer:String){
val uid = FirebaseAuth.getInstance().uid ?: ""
val ref = FirebaseDatabase.getInstance().getReference("/users/$emailer/profile_pic")
ref.setValue(profilepicURL)
.addOnSuccessListener {
Log.d("msg0", "saved pic to firebase database, $ref")
}
.addOnFailureListener{
Log.d("msg0", "failed to save pic into database, ${it.message}")
}
}
private fun chooseImage() {
val intent = Intent(Intent.ACTION_PICK)
intent.type = MediaStore.Images.Media.CONTENT_TYPE
startActivityForResult(intent, 10)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
val emailer=intent.getStringExtra("passemail")
if (requestCode == 10 && resultCode == Activity.RESULT_OK && data != null) {
profileURi = data.data
val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, profileURi)
Log.d("msg0 profileuri", profileURi.toString());
profilepic.setImageBitmap(bitmap)
uploadImagetoFirebase(emailer);
}
}
private fun uploadImagetoFirebase(emailer:String){
if(profileURi == null) return
val filename = UUID.randomUUID().toString()
val ref = FirebaseStorage.getInstance().getReference("/images/$filename")
ref.putFile(profileURi!!)
.addOnSuccessListener {
Toast.makeText(this, "Successfully uploaded image ${it.metadata?.path}", Toast.LENGTH_SHORT).show()
Log.d("msg0 upload", "image url: " + it.metadata?.path);
ref.downloadUrl.addOnSuccessListener {
Log.d("msg1 upload", "image url: " + it);
Toast.makeText(this, "File Location $it", Toast.LENGTH_SHORT).show()
saveUserToDatabase(it.toString(),emailer)
}
}
}
override fun onBackPressed() {
val intent = Intent(this, MainActivity :: class.java)
Log.d("checker2", emailz)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK.or(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.putExtra("passemail",emailz)
startActivity(intent)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.profile_page)
val emailer=intent.getStringExtra("passemail")
// Initialize Firebase Auth
auth = FirebaseAuth.getInstance()
emailz = emailer
//Create Image View and set it to call upload when clicked
profilepic = findViewById<de.hdodenhof.circleimageview.CircleImageView>(R.id.img_profile)
val uid = FirebaseAuth.getInstance().uid ?: ""
val ref = FirebaseDatabase.getInstance().getReference("/users/$emailer")
val valueEventListener = object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
val user = dataSnapshot.getValue(User::class.java)
Log.d("msg0 profile", "image url: " + user!!.profile_pic);
if(user!!.profile_pic.length > 0){
Picasso.get().load(user!!.profile_pic).into(profilepic)
}
}
override fun onCancelled(databaseError: DatabaseError) {
Log.d("msg0", databaseError.message) //Don't ignore errors!
}
}
ref.addListenerForSingleValueEvent(valueEventListener)
profilepic.setOnClickListener(View.OnClickListener { chooseImage() })
}
}
我正在使用图像按钮库,但是在添加它之前就存在此问题。
答案 0 :(得分:0)
您不需要启动新的MainActivity
,因为
如果您单击后退按钮,ProfilePage
将被破坏,并且您将看到MainActivity
。
如果ValueEventListener
中的MainActivity
工作正常,则可以正确看到图片。
希望获得帮助