从画廊的乐趣中选择图像,如下所示:
private fun selectImageFromGallery() { val intent = Intent() intent.type = "image/*" intent.action = Intent.ACTION_GET_ConTENT startActivityForResult( Intent.createChooser( intent, "Please select..." ), GALLERY_REQUEST_CODE )}onActivityResult像这样:
override fun onActivityResult( requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult( requestCode, resultCode, data ) if (requestCode == GALLERY_REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null && data.data != null ) { // Get the Uri of data val file_uri = data.data img_adu.setImageURI(file_uri) img_adu.visibility = View.VISIBLE bitmap = file_uri?.getCapturedImage(applicationContext) val stream = ByteArrayOutputStream() bitmap?.compress(Bitmap.CompressFormat.JPEG, 100, stream) bitmap?.recycle() val array = stream.toByteArray() enpred_string = base64.enpreToString(array, 0) } }获取像这样的位图扩展名:
fun Uri.getCapturedImage(context: Context): Bitmap? {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { val source = context?.contentResolver?.let { ImageDeprer.createSource(it, this) } return source?.let { ImageDeprer.depreBitmap(it) }} else { return MediaStore.Images.Media.getBitmap( context?.contentResolver, this ) }}祝好运



