Android Q: 从相册获取图片并进行处理
创始人
2024-08-15 02:30:08
0

下面是一个使用Android Q从相册获取图片并进行处理的解决方法,包含代码示例:

  1. 添加权限和依赖 在AndroidManifest.xml文件中添加以下权限:


在build.gradle文件中添加以下依赖:

implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'com.github.bumptech.glide:glide:4.11.0'
implementation 'com.github.bumptech.glide:compiler:4.11.0'
  1. 创建一个工具类来处理图片选择逻辑
import android.app.Activity
import android.content.ContentResolver
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import android.webkit.MimeTypeMap
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment

object ImagePickerUtils {

    private const val REQUEST_PICK_IMAGE = 100

    private var onImagePickedListener: ((Uri?) -> Unit)? = null

    fun pickImage(activity: Activity, pickImageListener: (Uri?) -> Unit) {
        onImagePickedListener = pickImageListener
        val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
        activity.startActivityForResult(intent, REQUEST_PICK_IMAGE)
    }

    fun pickImage(fragment: Fragment, pickImageListener: (Uri?) -> Unit) {
        onImagePickedListener = pickImageListener
        val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
        fragment.startActivityForResult(intent, REQUEST_PICK_IMAGE)
    }

    fun handleActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == REQUEST_PICK_IMAGE && resultCode == Activity.RESULT_OK) {
            val selectedImage = data?.data
            onImagePickedListener?.invoke(selectedImage)
        } else {
            onImagePickedListener?.invoke(null)
        }
        onImagePickedListener = null
    }
}
  1. 在Activity或Fragment中调用图片选择方法并处理返回结果
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import com.bumptech.glide.Glide

class MainActivity : AppCompatActivity() {

    private lateinit var imageView: ImageView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        imageView = findViewById(R.id.image_view)
        val pickImageButton: Button = findViewById(R.id.pick_image_button)
        pickImageButton.setOnClickListener {
            ImagePickerUtils.pickImage(this) { uri ->
                // 处理选择的图片
                uri?.let {
                    Glide.with(this)
                        .load(it)
                        .into(imageView)
                }
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        ImagePickerUtils.handleActivityResult(requestCode, resultCode, data)
    }
}

这是一个简单的示例,展示了如何使用Android Q从相册获取图片并使用Glide库将其显示在ImageView中。你可以根据自己的需求进行修改和扩展。

相关内容

热门资讯

在玩家背景下!poker ma... 在玩家背景下!poker master辅助,hhpoker有没有辅助,窍门教程(有挂助手)-哔哩哔哩...
出现新变化!wepokerh5... 出现新变化!wepokerh5破解,hhpoker透视脚本,模板教程(有挂猫腻)-哔哩哔哩进入游戏-...
据目击者称!四川换三张辅助器,... 据目击者称!四川换三张辅助器,微信开心泉州辅助,总结教程(其实确实有挂的)-哔哩哔哩微信开心泉州辅助...
此事引发广泛关注!wepoke... 此事引发广泛关注!wepoker辅助是真的假的,哈糖大菠萝有挂吗,烘培教程(发现有挂)-哔哩哔哩1、...
反观!起点休闲辅助,小闲辅助软... 您好,起点休闲辅助这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩家在...
为了进一步!德普之星透视辅助软... 为了进一步!德普之星透视辅助软件,wepoker透视脚本免费使用视频,妙招教程(有挂助手)-哔哩哔哩...
出现新变化!皮皮跑子胡子,微信... 出现新变化!皮皮跑子胡子,微信微乐游戏辅助脚本,手段教程(原来真的有挂的)-哔哩哔哩1、进入游戏-大...
相较于以往!we poker游... 相较于以往!we poker游戏下,hhpoker怎么防辅助,方案教程(有挂解密)-哔哩哔哩1、每一...
最新消息!新道游开挂辅助透视,... 您好,新道游开挂辅助透视这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多...
据报道!哈糖大菠萝免费辅助器,... 据报道!哈糖大菠萝免费辅助器,wepoker轻量版有透视吗,要领教程(有挂教程)-哔哩哔哩;1、哈糖...