| | |
| | | import android.os.Bundle |
| | | import android.provider.MediaStore |
| | | import android.view.View |
| | | import android.widget.ProgressBar |
| | | import android.widget.Toast |
| | | import androidx.activity.result.contract.ActivityResultContracts |
| | | import androidx.appcompat.app.AlertDialog |
| | | import androidx.appcompat.app.AppCompatActivity |
| | | import androidx.core.app.ActivityCompat |
| | | import androidx.core.content.ContextCompat |
| | | import androidx.lifecycle.lifecycleScope |
| | | import com.bumptech.glide.Glide |
| | | import com.example.firstapp.database.response.UserInfo |
| | | import com.example.firstapp.databinding.ActivityEditProfileBinding |
| | | import com.example.firstapp.database.service.ApiService |
| | | import com.example.firstapp.database.service.RetrofitClient |
| | | import com.example.firstapp.utils.PreferencesManager |
| | | import kotlinx.coroutines.launch |
| | | import java.io.File |
| | | import okhttp3.MediaType.Companion.toMediaType |
| | | import okhttp3.MultipartBody |
| | | import okhttp3.RequestBody |
| | | import okhttp3.RequestBody.Companion.asRequestBody |
| | | import okhttp3.RequestBody.Companion.toRequestBody |
| | | |
| | | class EditProfileActivity : AppCompatActivity() { |
| | | private lateinit var binding: ActivityEditProfileBinding |
| | | 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() |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | binding.ivAvatar.setOnClickListener { |
| | | checkAndRequestPermission() |
| | | } |
| | | |
| | | binding.btnSaveBottom.setOnClickListener { |
| | | saveAndFinish() |
| | | // checkAndRequestPermission() |
| | | checkStoragePermission() |
| | | } |
| | | |
| | | binding.btnSaveBottom.setOnClickListener { |
| | |
| | | } |
| | | } |
| | | |
| | | private fun saveAndFinish() { |
| | | val newNickname = binding.etNickname.text.toString() |
| | | if (newNickname.isEmpty()) { |
| | | Toast.makeText(this, "昵称不能为空", Toast.LENGTH_SHORT).show() |
| | | return |
| | | fun checkStoragePermission() { |
| | | val permission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { |
| | | Manifest.permission.READ_MEDIA_IMAGES |
| | | } else { |
| | | Manifest.permission.READ_EXTERNAL_STORAGE |
| | | } |
| | | |
| | | val resultIntent = Intent().apply { |
| | | putExtra("nickname", newNickname) |
| | | putExtra("avatar_uri", selectedImageUri?.toString()) |
| | | if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { |
| | | permissionLauncher.launch(permission) |
| | | } else { |
| | | openGallery() |
| | | } |
| | | setResult(Activity.RESULT_OK, resultIntent) |
| | | finish() |
| | | } |
| | | |
| | | |
| | | private fun saveAndFinish() { |
| | | lifecycleScope.launch { |
| | | try { |
| | | binding.btnSaveBottom.isEnabled = false |
| | | showLoading() |
| | | |
| | | val newNickname = binding.etNickname.text.toString() |
| | | if (newNickname.isEmpty()) { |
| | | Toast.makeText(this@EditProfileActivity, "昵称不能为空", Toast.LENGTH_SHORT).show() |
| | | return@launch |
| | | } |
| | | |
| | | // 准备文件和参数 |
| | | val nicknameBody = newNickname.toRequestBody("text/plain".toMediaType()) |
| | | |
| | | // 如果选择了新头像,处理文件 |
| | | val avatarPart = selectedImageUri?.let { uri -> |
| | | val inputStream = contentResolver.openInputStream(uri) |
| | | val file = File(cacheDir, "avatar_temp") |
| | | inputStream?.use { input -> |
| | | file.outputStream().use { output -> |
| | | input.copyTo(output) |
| | | } |
| | | } |
| | | |
| | | val requestFile = file.asRequestBody("image/*".toMediaType()) |
| | | MultipartBody.Part.createFormData("avatar", file.name, requestFile) |
| | | } |
| | | |
| | | // 调用更新接口 |
| | | RetrofitClient.apiService.updateProfile( |
| | | nickname = nicknameBody, |
| | | avatar = avatarPart |
| | | ) |
| | | |
| | | Toast.makeText(this@EditProfileActivity, "保存成功", Toast.LENGTH_SHORT).show() |
| | | // 更新用户信息 |
| | | // 从本地获取保存的手机号 |
| | | val savedPhone = PreferencesManager.getPhone() |
| | | if (savedPhone.isNullOrEmpty()) { |
| | | Toast.makeText(this@EditProfileActivity, "用户未登录", Toast.LENGTH_SHORT).show() |
| | | return@launch |
| | | } |
| | | val response = RetrofitClient.apiService.getUserInfo(savedPhone) |
| | | if (response.code == "0" && response.data != null) { |
| | | // 保存用户信息 |
| | | val userInfo:UserInfo = response.data |
| | | // 获取传入的数据 |
| | | val currentNickname = userInfo.name |
| | | val currentAvatarUrl = userInfo.cover |
| | | |
| | | // 设置当前数据 |
| | | binding.etNickname.setText(currentNickname) |
| | | if (!currentAvatarUrl.isNullOrEmpty()) { |
| | | Glide.with(this@EditProfileActivity) |
| | | .load(currentAvatarUrl) |
| | | .circleCrop() |
| | | .into(binding.ivAvatar) |
| | | } |
| | | |
| | | } |
| | | |
| | | finish() |
| | | |
| | | } catch (e: Exception) { |
| | | Toast.makeText(this@EditProfileActivity, "保存失败: ${e.message}", Toast.LENGTH_SHORT).show() |
| | | } finally { |
| | | binding.btnSaveBottom.isEnabled = true |
| | | hideLoading() |
| | | } |
| | | } |
| | | } |
| | | |
| | | private fun checkAndRequestPermission() { |
| | |
| | | } |
| | | } |
| | | |
| | | 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() { |
| | | if (loadingDialog == null) { |
| | | loadingDialog = AlertDialog.Builder(this) |
| | | .setView(ProgressBar(this)) |
| | | .setCancelable(false) |
| | | .create() |
| | | } |
| | | loadingDialog?.show() |
| | | } |
| | | |
| | | private fun hideLoading() { |
| | | loadingDialog?.dismiss() |
| | | } |
| | | |
| | | private fun String.toRequestBody(mediaType: String): RequestBody { |
| | | return this.toRequestBody(mediaType.toMediaType()) |
| | | } |
| | | |
| | | private fun File.asRequestBody(mediaType: String): RequestBody { |
| | | return this.asRequestBody(mediaType.toMediaType()) |
| | | } |
| | | |
| | | companion object { |