安卓中如何访问系统相册

效果图

主要代码

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        button.setOnClickListener { applyPermissionAndOpenAlbum() }
    }

    private fun applyPermissionAndOpenAlbum() {
        SoulPermission.getInstance()
            .checkAndRequestPermissions(
                Permissions.build(
                    Manifest.permission.READ_EXTERNAL_STORAGE
                ),
                object : CheckRequestPermissionsListener {
                    override fun onAllPermissionOk(allPermissions: Array?) {
                        openAlbum()
                    }

                    override fun onPermissionDenied(refusedPermissions: Array?) {
                    }
                })
    }

    private fun openAlbum() {
        val intent = Intent()
        intent.action = Intent.ACTION_GET_CONTENT
        intent.type = "image/*"
        startActivityForResult(intent, 2)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == 2 && resultCode == RESULT_OK) {
            data?.data?.let {
                Glide.with(this).load(it).into(imageView)
            }
        }
        super.onActivityResult(requestCode, resultCode, data)
    }
}

完整源代码

https://gitee.com/hspbc/camera_album_demo

你可能感兴趣的:(安卓中如何访问系统相册)