写一段 代码 ,将从相册里面选择的相片,加载 显示 到界面 里面,可以多次选择,显示 多张图片,用kotlin实现
时间: 2024-10-12 15:11:03 浏览: 44
在Android应用中,我们可以使用`ACTION_PICK`或`ACTION_GET_CONTENT`系统 intent 来选择照片,并使用 `GalleyProvider` 或 `MediaStore` API 来访问相册。下面是一个简单的Kotlin示例,展示了如何在活动中选择并显示多张图片:
```kotlin
import android.content.Intent
import android.content.res.Configuration
import android.graphics.Bitmap
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import androidx.appcompat.app.AppCompatActivity
import android.view.View
import android.widget.Button
import android.widget.GridView
import java.io.File
class ImagePickerController : AppCompatActivity() {
private var imageAdapter: GridViewImageAdapter? = null
private lateinit var gridView: GridView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_image_picker)
gridView = findViewById(R.id.gridView)
gridView.layoutManager = GridLayoutManager(this, 2) // 设置网格布局
val pickButton = findViewById<Button>(R.id.pick_button)
pickButton.setOnClickListener {
selectImages()
}
}
private fun selectImages() {
val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(intent, REQUEST_IMAGE_PICK)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_IMAGE_PICK && resultCode == RESULT_OK) {
if (data != null) {
val selectedPhotos = data?.getData()?.let { Uri.parse(it.toString()) }?.let { MediaStore.Images.Media.EXTERNAL_CONTENT_URI.withQuery(null, "selected=1", null) }
loadAndDisplayImages(selectedPhotos)
}
}
}
private fun loadAndDisplayImages(imagesUri: Uri?) {
if (imageAdapter == null) {
imageAdapter = GridViewImageAdapter(this)
gridView.adapter = imageAdapter
}
if (imagesUri != null) {
val cursor = getContentResolver().query(imagesUri, arrayOf(MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA), null, null, null)
if (cursor != null) {
while (cursor.moveToNext()) {
val id = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media._ID))
val filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA))
val bitmap = BitmapFactory.decodeFile(filePath)
imageAdapter!!.addImage(id, bitmap)
}
cursor.close()
}
}
}
class GridViewImageAdapter(private val context: Context) : BaseAdapter() {
private val images = mutableListOf<Bitmap>()
// 其他相关方法,如 getItem(), getCount(), etc.
override fun addImage(id: Long, bitmap: Bitmap) {
images.add(bitmap)
notifyDataSetChanged()
}
// 返回视图,包括设置 ImageView 的 Bitmap
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
//...
}
}
}
// 请求码
const val REQUEST_IMAGE_PICK = 100
```
在这个例子中,我们首先在Activity中设置了GridView用于显示图像,然后在点击“选择”按钮时启动一个Intent去获取用户选择的照片。当用户选择后,我们通过查询数据库获取选中的图片路径,然后使用`BitmapFactory.decodeFile`解码图片并添加到adapter中,最后更新GridView显示新的图片。注意,为了处理多次选择,你需要在`GridViewImageAdapter`中维护一个Bitmap集合,并在需要时更新显示。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![.zip](https://img-home.csdnimg.cn/images/20241231045053.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/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)