修改头像
This commit is contained in:
@@ -4,8 +4,6 @@ import android.Manifest
|
||||
import android.content.ContentResolver
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
@@ -26,16 +24,19 @@ import com.example.defenseapplication.R
|
||||
import com.example.defenseapplication.bean.UpdateUserInfo
|
||||
import com.example.defenseapplication.bean.UserInfoBean
|
||||
import com.example.defenseapplication.databinding.PersonalProfileActivityBinding
|
||||
import com.example.defenseapplication.utils.OssUtil
|
||||
import com.example.defenseapplication.utils.RetrofitNetwork
|
||||
import com.example.defenseapplication.view.AvatarSelectDialog
|
||||
import com.example.defenseapplication.view.CustomDialogFragmentText
|
||||
import com.example.defenseapplication.view.LoadingDialog
|
||||
import com.example.defenseapplication.view.PhoneModifyDialogFragment
|
||||
import com.gyf.immersionbar.ImmersionBar
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
||||
import okhttp3.MultipartBody
|
||||
import okhttp3.RequestBody.Companion.asRequestBody
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
@@ -51,6 +52,9 @@ class PersonalProfileActivity : AppCompatActivity() {
|
||||
// 拍照返回的文件
|
||||
private var photoFile: File? = null
|
||||
|
||||
// 加载弹窗
|
||||
private lateinit var loadingDialog: LoadingDialog
|
||||
|
||||
// 请求权限回调
|
||||
private val requestPermissionsLauncher = registerForActivityResult(
|
||||
ActivityResultContracts.RequestMultiplePermissions()
|
||||
@@ -116,6 +120,8 @@ class PersonalProfileActivity : AppCompatActivity() {
|
||||
.statusBarDarkFont(true)
|
||||
.titleBar(binding.topBar)
|
||||
.init()
|
||||
// 初始化加载弹窗
|
||||
loadingDialog = LoadingDialog(this)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
userInfo = intent.getParcelableExtra("userInfo", UserInfoBean::class.java)
|
||||
} else {
|
||||
@@ -327,7 +333,6 @@ class PersonalProfileActivity : AppCompatActivity() {
|
||||
".jpg",
|
||||
storageDir
|
||||
).apply {
|
||||
// 保存文件路径以便后续使用
|
||||
Log.d("PersonalProfile", "创建图片文件: $absolutePath")
|
||||
}
|
||||
}
|
||||
@@ -352,7 +357,7 @@ class PersonalProfileActivity : AppCompatActivity() {
|
||||
val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
|
||||
val storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
|
||||
val file = File(storageDir, "JPEG_${timeStamp}_temp.jpg")
|
||||
|
||||
|
||||
FileOutputStream(file).use { outputStream ->
|
||||
val buffer = ByteArray(1024)
|
||||
var bytesRead: Int
|
||||
@@ -370,18 +375,34 @@ class PersonalProfileActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传头像到 OSS 并更新个人信息
|
||||
* 上传头像到服务器并更新个人信息
|
||||
*/
|
||||
private fun uploadAvatar(filePath: String) {
|
||||
Toast.makeText(this, getString(R.string.uploading), Toast.LENGTH_SHORT).show()
|
||||
// 显示加载弹窗
|
||||
loadingDialog.show()
|
||||
|
||||
OssUtil.uploadFileAsync(this, filePath, "avatar") { ossUrl ->
|
||||
runOnUiThread {
|
||||
if (ossUrl != null) {
|
||||
Log.d("PersonalProfile", "OSS 上传成功,URL: $ossUrl")
|
||||
updateAvatar(ossUrl)
|
||||
} else {
|
||||
Toast.makeText(this@PersonalProfileActivity, getString(R.string.upload_failed), Toast.LENGTH_SHORT).show()
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
try {
|
||||
val file = File(filePath)
|
||||
val requestBody = file.asRequestBody("image/jpeg".toMediaTypeOrNull())
|
||||
val part = MultipartBody.Part.createFormData("file", file.name, requestBody)
|
||||
|
||||
val uploadResponse = RetrofitNetwork.getNetworkService().uploadFile(part)
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
if (uploadResponse.code == 200 && uploadResponse.data?.url != null) {
|
||||
Log.d("PersonalProfile", "文件上传成功,URL: ${uploadResponse.data?.url}")
|
||||
updateAvatar(uploadResponse.data!!.url)
|
||||
} else {
|
||||
loadingDialog.dismiss()
|
||||
Toast.makeText(this@PersonalProfileActivity, getString(R.string.upload_failed), Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
withContext(Dispatchers.Main) {
|
||||
loadingDialog.dismiss()
|
||||
Toast.makeText(this@PersonalProfileActivity, getString(R.string.network_error), Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -399,8 +420,8 @@ class PersonalProfileActivity : AppCompatActivity() {
|
||||
)
|
||||
val response = RetrofitNetwork.getNetworkService().updateUserInfo(request)
|
||||
withContext(Dispatchers.Main) {
|
||||
loadingDialog.dismiss()
|
||||
if (response.code == 200) {
|
||||
// 更新本地头像显示
|
||||
Glide.with(this@PersonalProfileActivity)
|
||||
.load(avatarUrl)
|
||||
.apply(RequestOptions()
|
||||
@@ -417,6 +438,7 @@ class PersonalProfileActivity : AppCompatActivity() {
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
withContext(Dispatchers.Main) {
|
||||
loadingDialog.dismiss()
|
||||
Toast.makeText(this@PersonalProfileActivity, getString(R.string.network_error), Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user