| | |
| | | import android.Manifest |
| | | import android.app.Activity |
| | | import android.content.Intent |
| | | import android.net.Uri |
| | | import android.os.Build |
| | | import android.os.Bundle |
| | | import android.provider.MediaStore |
| | | import android.util.Log |
| | | import android.view.View |
| | | import androidx.activity.result.PickVisualMediaRequest |
| | | import androidx.activity.result.contract.ActivityResultContracts |
| | | import cn.bingoogolapple.qrcode.core.BarcodeType |
| | | import cn.bingoogolapple.qrcode.core.QRCodeView |
| | | import cn.bingoogolapple.qrcode.zxing.ZXingView |
| | |
| | | titleBar.titleView.text = getString(R.string.扫一扫) |
| | | titleBar.titleView.textColorResource = cn.sinata.xldutils.R.color.white |
| | | titleBar.leftView.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.back_white,0,0,0) |
| | | // 启动Photo Picker选择图片(单选示例) |
| | | val pickMedia = registerForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri -> |
| | | if (uri != null) { |
| | | // 您拿到了图片的Uri,可以安全地使用它(读取、上传等) |
| | | // 注意:此访问是临时的,应用重启后可能需要重新选取 |
| | | handleSelectedImage(uri) |
| | | } |
| | | } |
| | | titleBar.addRightButton(getString(R.string.photo),onClickListener = View.OnClickListener { |
| | | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { |
| | | RxPermissions(this).request( |
| | | Manifest.permission.READ_MEDIA_IMAGES |
| | | ) |
| | | // 启动选择器 |
| | | pickMedia.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly)) |
| | | } else { |
| | | RxPermissions(this).request( |
| | | Manifest.permission.READ_EXTERNAL_STORAGE, |
| | | ) |
| | | }.subscribe { |
| | | ).subscribe { |
| | | if (it) { |
| | | val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)// 调用android的图库 |
| | | val intent = Intent( |
| | | Intent.ACTION_PICK, |
| | | MediaStore.Images.Media.EXTERNAL_CONTENT_URI |
| | | )// 调用android的图库 |
| | | intent.type = "image/*" |
| | | startActivityForResult(intent, 1) |
| | | }else{ |
| | | toast(getString(R.string.没有权限)) |
| | | } |
| | | } |
| | | } |
| | | }) |
| | | titleBar.getRightButton(0)?.textColorResource = cn.sinata.xldutils.R.color.white |
| | | initQRCode() |
| | | } |
| | | |
| | | private fun handleSelectedImage(uri: Uri) { |
| | | val path = getUrlPath(uri) |
| | | if (path != null) { |
| | | val typeIndex = path.lastIndexOf(".") |
| | | if (typeIndex != -1) { |
| | | val fileType = path.substring(typeIndex + 1).toLowerCase(Locale.CHINA) |
| | | //某些设备选择图片是可以选择一些非图片的文件。然后发送出去或出错。这里简单的通过匹配后缀名来判断是否是图片文件 |
| | | //如果是图片文件则发送。反之给出提示 |
| | | if (fileType == "jpg" || fileType == "gif" |
| | | || fileType == "png" || fileType == "jpeg" |
| | | || fileType == "bmp" || fileType == "wbmp" |
| | | || fileType == "ico" || fileType == "jpe") { |
| | | zxingview.decodeQRCode(path) |
| | | } else { |
| | | toast("无法识别的图片类型!") |
| | | } |
| | | } else { |
| | | toast("无法识别的图片类型!") |
| | | } |
| | | } else { |
| | | toast("无法识别的图片类型或路径!") |
| | | } |
| | | } |
| | | |
| | | private val zxingview by lazy { find<ZXingView>(R.id.zxingview) } |
| | |
| | | if (resultCode == Activity.RESULT_OK&&data!=null&&requestCode == 1){ |
| | | val uri = data.data |
| | | if (uri != null) { |
| | | val path = getUrlPath(uri) |
| | | if (path != null) { |
| | | val typeIndex = path.lastIndexOf(".") |
| | | if (typeIndex != -1) { |
| | | val fileType = path.substring(typeIndex + 1).toLowerCase(Locale.CHINA) |
| | | //某些设备选择图片是可以选择一些非图片的文件。然后发送出去或出错。这里简单的通过匹配后缀名来判断是否是图片文件 |
| | | //如果是图片文件则发送。反之给出提示 |
| | | if (fileType == "jpg" || fileType == "gif" |
| | | || fileType == "png" || fileType == "jpeg" |
| | | || fileType == "bmp" || fileType == "wbmp" |
| | | || fileType == "ico" || fileType == "jpe") { |
| | | zxingview.decodeQRCode(path) |
| | | } else { |
| | | toast("无法识别的图片类型!") |
| | | } |
| | | } else { |
| | | toast("无法识别的图片类型!") |
| | | } |
| | | } else { |
| | | toast("无法识别的图片类型或路径!") |
| | | } |
| | | handleSelectedImage(uri) |
| | | } else { |
| | | toast("无法识别的图片类型!") |
| | | } |