package com.example.newapplication.personal
import android.content.Context
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.example.newapplication.R
import com.example.newapplication.databinding.ActivityNameBinding
import com.example.newapplication.databinding.ActivityNoBinding
import com.example.personalwork.utils.ApiClient
import kotlinx.coroutines.launch
class NoActivity : AppCompatActivity() {
var _binding : ActivityNoBinding?=null
val binding get() = _binding!!
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_no)
_binding = ActivityNoBinding.inflate(layoutInflater)
init()
doSave()
setContentView(binding.root)
}
fun init(){
val sharedPreferences: SharedPreferences = this.getSharedPreferences("User", Context.MODE_PRIVATE)
val text = binding.editTextTextPersonName
text.setText(sharedPreferences.getString("userNo",""))
}
fun doSave() {
val sharedPreferences = this.getSharedPreferences("User", Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
val btn = binding.imageButton2
val text = binding.editTextTextPersonName
btn.setOnClickListener {
lifecycleScope.launch {
try {
val name = text.text.toString()
val response = ApiClient.apiService.changeNo(sharedPreferences.getInt("userId", 1).toString(), name)
val result = response.body()?.data
// 更新userName字段的值
editor.putString("userNo", name)
editor.apply() // 使用apply()而不是commit()
// 打印当前userName的值
Toast.makeText(baseContext,"成功", Toast.LENGTH_SHORT).show()
finish()
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}
package com.example.newapplication.personal
import android.content.Context
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.example.newapplication.R
import com.example.newapplication.databinding.ActivityNoBinding
import com.example.newapplication.databinding.ActivityPasswordBinding
import com.example.personalwork.utils.ApiClient
import kotlinx.coroutines.launch
class PasswordActivity : AppCompatActivity() {
var _binding : ActivityPasswordBinding?=null
val binding get() = _binding!!
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_password)
_binding = ActivityPasswordBinding.inflate(layoutInflater)
init()
doSave()
setContentView(binding.root)
}
fun init(){
val sharedPreferences: SharedPreferences = this.getSharedPreferences("User", Context.MODE_PRIVATE)
val text = binding.editTextTextPersonName
text.setText(sharedPreferences.getString("userPassword",""))
}
fun doSave() {
val sharedPreferences = this.getSharedPreferences("User", Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
val btn = binding.imageButton2
val text = binding.editTextTextPersonName
btn.setOnClickListener {
lifecycleScope.launch {
try {
val name = text.text.toString()
val response = ApiClient.apiService.changePassword(sharedPreferences.getInt("userId", 1).toString(), name)
val result = response.body()?.data
// 更新userName字段的值
editor.putString("userPassword", name)
editor.apply() // 使用apply()而不是commit()
// 打印当前userName的值
Toast.makeText(baseContext,"成功", Toast.LENGTH_SHORT).show()
finish()
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}
}
override fun onDestroy() {
super.onDestroy()
_binding = null
}
}
package com.example.newapplication.personal
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.provider.MediaStore
import android.util.Base64
import android.widget.FrameLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.FileProvider
import androidx.lifecycle.lifecycleScope
import com.example.newapplication.HomeActivity
import com.example.newapplication.MainActivity
import com.example.newapplication.R
import com.example.newapplication.databinding.ActivityPersonalBinding
import com.example.personalwork.utils.ApiClient
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.yalantis.ucrop.UCrop
import kotlinx.coroutines.launch
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.*
class PersonalActivity : AppCompatActivity() {
var _binding : ActivityPersonalBinding?=null
val binding get() = _binding!!
private var currentPhotoPath: String? = null
private val PICK_IMAGE_REQUEST = 1
private val REQUEST_CODE_UCROP = 2
private val REQUEST_IMAGE_CAPTURE = 3
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_personal)
_binding = ActivityPersonalBinding.inflate(layoutInflater)
init()
setMessage()
changeMessage()
setContentView(binding.root)
}
fun init(){
val btnavator = binding.avator
btnavator.setOnClickListener{
val bottomSheetDialog = BottomSheetDialog(this)
bottomSheetDialog.setContentView(R.layout.bottom_sheet)
val cameraOption = bottomSheetDialog.findViewById<TextView>(R.id.camera_option)
val galleryOption = bottomSheetDialog.findViewById<TextView>(R.id.gallery_option)
val cancelOption = bottomSheetDialog.findViewById<TextView>(R.id.cancel_option)
cameraOption?.setOnClickListener {
openCamera()
bottomSheetDialog.dismiss()
}
galleryOption?.setOnClickListener {
openGallery()
bottomSheetDialog.dismiss()
}
cancelOption?.setOnClickListener {
// 在这里处理点击取消的逻辑
val storageDir: File? = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
System.out.println(storageDir)
bottomSheetDialog.dismiss()
}
bottomSheetDialog.show()
}
}
//打开相册
fun openGallery() {
val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(intent, PICK_IMAGE_REQUEST)
}
//打开相机
fun openCamera() {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
takePictureIntent.resolveActivity(packageManager).also {
val photoFile: File? = createImageFile()
photoFile?.also {
val photoUri = FileProvider.getUriForFile(this, "com.example.newapplication.fileprovider", it)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri)
startActivityForResult(takePictureIntent,REQUEST_IMAGE_CAPTURE) // 修改这里的 requestCode
}
}
}
// 创建保存图片的文件
@Throws(IOException::class)
private fun createImageFile(): File? {
System.out.println("这也被调用了")
val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
val storageDir: File? = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
System.out.println(storageDir)
val file = File.createTempFile(
"JPEG_${timeStamp}_",
".jpg",
storageDir
)
currentPhotoPath = file.absolutePath
return file
}
// 剪裁图片
fun cropImage(uri: Uri) {
val destinationUri = Uri.fromFile(File(cacheDir, "cropped_image"))
UCrop.of(uri, destinationUri)
.withAspectRatio(1f, 1f)
.start(this, REQUEST_CODE_UCROP)
}
// 上传头像
fun uploadImage(uri: Uri) {
val file = File(uri.path)
val sharedPreferences: SharedPreferences = this.getSharedPreferences("User", Context.MODE_PRIVATE)
val requestFile = RequestBody.create(MediaType.parse("image/*"), file)
val body = MultipartBody.Part.createFormData("image", file.name, requestFile)
lifecycleScope.launch {
try {
val no = sharedPreferences.getString("userNo","").toString()
val response = ApiClient.apiService.uploadImage(body,no)
val result = response.body()?.data
System.out.println(result)
if(result != null){
stringToImage(result)
System.out.println("成功")
}else{
System.out.println("失败")
}
}catch (e:Exception){
e.printStackTrace()
}
}
}
//把数据库内容转换成图片
fun stringToImage(image:String){
val avatar = binding.fragmentAvatar
val byteArrayString = image // 你的字节数组数据
val imageBytes = Base64.decode(byteArrayString, Base64.DEFAULT)
// 将字节数组转换为 Bitmap
val bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
saveImage(image)
avatar.setImageBitmap(bitmap)
}
//保存图片到 sharepreference
fun saveImage(image: String){
val sharedPreferences = getSharedPreferences("my_shared_prefs", Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.putString("bitmap_key", image)
editor.apply()
}
// 处理选择图片结果
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK && data != null) {
val uri = data.data
uri?.let { cropImage(it) }
} else if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK && data != null){
// 拍照成功后处理
val photoFile = File(currentPhotoPath)
val photoUri = FileProvider.getUriForFile(this, "com.example.newapplication.fileprovider", photoFile)
cropImage(photoUri) // 在这里启动剪辑操作
}
if (requestCode == REQUEST_CODE_UCROP && resultCode == Activity.RESULT_OK && data != null) {
val resultUri = UCrop.getOutput(data)
resultUri?.let { uploadImage(it) }
}
}
//设置角色信息
fun setMessage(){
val sharedPreferences: SharedPreferences = this.getSharedPreferences("User", Context.MODE_PRIVATE)
val shareImage : SharedPreferences = this.getSharedPreferences("my_shared_prefs",Context.MODE_PRIVATE)
val byteArrayString = shareImage.getString("bitmap_key","")
val name = binding.name
val no = binding.no
//设置头像
setImage(byteArrayString.toString())
name.setText(sharedPreferences.getString("userName",""))
no.setText(sharedPreferences.getString("userNo",""))
}
//设置头衔
fun setImage(image:String){
val byteArrayString = image
val imageBytes = Base64.decode(byteArrayString, Base64.DEFAULT)
// 将字节数组转换为 Bitmap
val bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
val avatar = binding.fragmentAvatar
avatar.setImageBitmap(bitmap)
}
//设置修改功能
fun changeMessage(){
changeName()
changeNo()
changePassword()
changeAddress()
changeInterest()
exit()
}
//修改名字
fun changeName(){
val btnName = binding.fragmentName
btnName.setOnClickListener{
val intent = Intent(this,NameActivity::class.java)
startActivity(intent)
}
}
//修改账号
fun changeNo(){
val btnNo = binding.fragmentNo
btnNo.setOnClickListener{
val intent = Intent(this,NoActivity::class.java)
startActivity(intent)
}
}
//修改密码
fun changePassword(){
val btnPassword = binding.fragmentPassword
btnPassword.setOnClickListener{
val intent = Intent(this,PasswordActivity::class.java)
startActivity(intent)
}
}
//修改地址
fun changeAddress(){
val btnAddress = binding.fragmentAddress
btnAddress.setOnClickListener{
val intent = Intent(this,AddressActivity::class.java)
startActivity(intent)
}
}
//修改爱好
fun changeInterest(){
val btnInterest = binding.fragmentInterest
btnInterest.setOnClickListener{
val intent = Intent(this,InterestActivity::class.java)
startActivity(intent)
}
}
//退出到登录界面
fun exit(){
val btnExit = binding.fragmentExit
btnExit.setOnClickListener{
clearSharedPreferences()
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
}
//清空本地存储
fun clearSharedPreferences() {
val sharedPreferences = this.getSharedPreferences("User", MODE_PRIVATE)
val sharedImage = this.getSharedPreferences("my_shared_prefs", MODE_PRIVATE)
val editor1 = sharedImage.edit()
val editor = sharedPreferences.edit()
editor.clear() // 清空SharedPreferences
editor1.clear()
editor.apply() // 应用更改
editor1.clear()
}
override fun onDestroy() {
super.onDestroy()
_binding = null
}
}
package com.example.newapplication.personal
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.newapplication.R
class RecordingActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_recording)
}
}