From a873764794630f726aba27ce4f3c67ae8c62289c Mon Sep 17 00:00:00 2001
From: zhujie <leon.zhu@cloudroam.com.cn>
Date: 星期五, 18 四月 2025 17:40:32 +0800
Subject: [PATCH] 样式调整
---
app/src/main/java/com/example/firstapp/ui/profile/EditProfileActivity.kt | 73 ++++++++++++++++++++++++++++++------
1 files changed, 61 insertions(+), 12 deletions(-)
diff --git a/app/src/main/java/com/example/firstapp/ui/profile/EditProfileActivity.kt b/app/src/main/java/com/example/firstapp/ui/profile/EditProfileActivity.kt
index a7f4af1..608eef6 100644
--- a/app/src/main/java/com/example/firstapp/ui/profile/EditProfileActivity.kt
+++ b/app/src/main/java/com/example/firstapp/ui/profile/EditProfileActivity.kt
@@ -36,15 +36,41 @@
private var selectedImageUri: Uri? = null
private var loadingDialog: AlertDialog? = null
- private val pickImage = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
- if (result.resultCode == Activity.RESULT_OK) {
- result.data?.data?.let { uri ->
- selectedImageUri = uri
- Glide.with(this)
- .load(uri)
- .circleCrop()
- .into(binding.ivAvatar)
- }
+// private val pickImage = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
+// if (result.resultCode == Activity.RESULT_OK) {
+// result.data?.data?.let { uri ->
+// selectedImageUri = uri
+// Glide.with(this)
+// .load(uri)
+// .circleCrop()
+// .into(binding.ivAvatar)
+// }
+// }
+// }
+
+ private val pickImageLauncher = registerForActivityResult(
+ ActivityResultContracts.GetContent()
+ ) { uri: Uri? ->
+ uri?.let {
+ selectedImageUri = uri
+ // 这里直接处理选中的头像
+ Glide.with(this)
+ .load(it)
+ .into(binding.ivAvatar) // 替换成你的 ImageView id
+
+ // 如果需要上传可以用 contentResolver.openInputStream(uri)
+ }
+ }
+
+
+ // 👇 就放在这里
+ private val permissionLauncher = registerForActivityResult(
+ ActivityResultContracts.RequestPermission()
+ ) { isGranted: Boolean ->
+ if (isGranted) {
+ openGallery()
+ } else {
+ Toast.makeText(this, "需要权限才能选择头像", Toast.LENGTH_SHORT).show()
}
}
@@ -78,13 +104,29 @@
}
binding.ivAvatar.setOnClickListener {
- checkAndRequestPermission()
+// checkAndRequestPermission()
+ checkStoragePermission()
}
binding.btnSaveBottom.setOnClickListener {
saveAndFinish()
}
}
+
+ fun checkStoragePermission() {
+ val permission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
+ Manifest.permission.READ_MEDIA_IMAGES
+ } else {
+ Manifest.permission.READ_EXTERNAL_STORAGE
+ }
+
+ if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
+ permissionLauncher.launch(permission)
+ } else {
+ openGallery()
+ }
+ }
+
private fun saveAndFinish() {
lifecycleScope.launch {
@@ -194,9 +236,16 @@
}
}
- private fun openGallery() {
+ private fun openGallery_bak() {
val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
- pickImage.launch(intent)
+// pickImage.launch(intent)
+ }
+
+ private fun openGallery() {
+ pickImageLauncher.launch("image/*")
+// val intent = Intent(Intent.ACTION_PICK)
+// intent.type = "image/*"
+// startActivityForResult(intent, 1001)
}
private fun showLoading() {
--
Gitblit v1.9.3