startActivityForResult(m_intent, CAMERA_REQUEST) 请求返回零,同时从相机成功捕获照片

问题描述 投票:0回答:1

Intent m_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 文件 file = new File(Environment.getExternalStorageDirectory(), "MyPhoto.jpg"); Uri uri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", file); m_intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri); startActivityForResult(m_intent, CAMERA_REQUEST);

android image-capture
1个回答
0
投票

DeGM Narmadapuram,在这里我分享了从相机单击图像并将其显示到图像视图的完整代码。


try {
                val chooserIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
                captureFile = createImageFile(this@TIDProcessActivity)

                if (captureFile != null) {

                    val photoURI = FileProvider.getUriForFile(
                        this@TIDProcessActivity,
                        "com.abcdemo.fileprovider",
                        captureFile
                    )

                    chooserIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
                }
                startActivityIntent.launch(chooserIntent)
            } catch (e: Exception) {
                Log.e("TAG", "onCreate: " + e.message)
            }

通过此方法,您可以捕获图片,并且该图片在单击图像时已创建 URI,因此当您获得结果时,您可以直接显示来自此 URI 的图像,如下所示:


var startActivityIntent = registerForActivityResult<Intent, ActivityResult>(
        ActivityResultContracts.StartActivityForResult()
    ) {
        
        val bitmap = BitmapFactory.decodeFile(captureFile.absolutePath)
        if (bitmap != null) {
            val fileName = System.currentTimeMillis().toString()
            Glide.with(this).load(bitmap).into(binding.zeroTokenImagePath)
        }

    }

© www.soinside.com 2019 - 2024. All rights reserved.