Android kotlin 用户点击更换头像 拍照和相册选择
时间: 2023-11-24 20:45:14 浏览: 329
以下是一个使用Kotlin编写的示例代码,用于允许用户更改其头像。它将打开一个对话框,让用户选择从相机拍照或从相册中选择。选择后,它将更新用户的头像。
```
private val REQUEST_IMAGE_CAPTURE = 1
private val REQUEST_IMAGE_SELECT = 2
private var mCurrentPhotoPath: String = ""
private fun changeAvatar() {
val pictureDialog = AlertDialog.Builder(this)
pictureDialog.setTitle("选择头像")
val pictureDialogItems = arrayOf("从相册选择", "拍照")
pictureDialog.setItems(pictureDialogItems
) { _, which ->
when (which) {
0 -> choosePhotoFromGallery()
1 -> takePhoto()
}
}
pictureDialog.show()
}
private fun choosePhotoFromGallery() {
val galleryIntent = Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(galleryIntent, REQUEST_IMAGE_SELECT)
}
private fun takePhoto() {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
if (takePictureIntent.resolveActivity(packageManager) != null) {
val photoFile: File? = try {
createImageFile()
} catch (ex: IOException) {
null
}
photoFile?.also {
val photoURI: Uri = FileProvider.getUriForFile(
this,
"com.example.android.fileprovider",
it
)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
}
}
}
@Throws(IOException::class)
private fun createImageFile(): File {
// Create an image file name
val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
val storageDir: File = getExternalFilesDir(Environment.DIRECTORY_PICTURES)!!
return File.createTempFile(
"JPEG_${timeStamp}_", /* prefix */
".jpg", /* suffix */
storageDir /* directory */
).apply {
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = absolutePath
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK) {
when (requestCode) {
REQUEST_IMAGE_CAPTURE -> {
val file = File(mCurrentPhotoPath)
val uri = Uri.fromFile(file)
updateAvatar(uri)
}
REQUEST_IMAGE_SELECT -> {
val uri = data?.data
updateAvatar(uri)
}
}
}
}
private fun updateAvatar(uri: Uri?) {
// Do something with the selected image
}
```
请注意,在使用相机拍照时,我们需要在`AndroidManifest.xml`文件中添加以下内容,以确保我们的应用程序具有访问相机的权限:
```
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
```
同时,我们还需要添加以下内容,以确保我们的应用程序具有访问存储的权限:
```
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)