0

WhoDoctor was helped me on the Previous code

Now, the android application crashed when during running on my phone and This is the error log

First problem is after I scanned the QRcode with camera it cannot show at the QRcode's result into the tvResult

Second problem is after I selected a QRcode image from the storage, then tap confirmed, it crash

Here below I think are the issue

Type mismatch: inferred type is Uri? but Uri was expected

galleryLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult(), object :ActivityResultCallback<ActivityResult>{
            override fun onActivityResult(result: ActivityResult?) {
                val data=result?.data
                inputImage = InputImage.fromFilePath(requireContext(), data?.data)
                processQr()
            }
        })

Redundant SAM-constructor

binding.btnScanBarcode.setOnClickListener{
            val options=arrayOf("camera","gallery")

            val builder=AlertDialog.Builder(requireContext())
            builder.setTitle("Pick a option")

            builder.setItems(options, DialogInterface.OnClickListener { dialog, which ->
                if(which==0){
                    val cameraIntent=Intent(MediaStore.ACTION_IMAGE_CAPTURE)
                    cameraLauncher.launch(cameraIntent)
                }else{
                    val storageIntent=Intent()
                    storageIntent.setType("image/*")
                    storageIntent.setAction(Intent.ACTION_GET_CONTENT)
                    galleryLauncher.launch(storageIntent)
                }
            })
            builder.show()
        }

'onRequestPermissionsResult(Int, Array<(out) String!>, IntArray): Unit' is deprecated. Deprecated in Java

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)

        if(requestCode==CAMERA_PERMISSION_CODE){
            if(grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                checkPermission(
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    READ_STORAGE_PERMISSION_CODE)
            }else{
                Toast.makeText(requireContext(), "Camera Permission Denied", Toast.LENGTH_SHORT).show()
            }
        }else if(requestCode==READ_STORAGE_PERMISSION_CODE){
            if((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)){
                checkPermission(
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    WRITE_STORAGE_PERMISSION_CODE)
            }else{
                Toast.makeText(requireContext(), "Storage Permission Denied", Toast.LENGTH_SHORT).show()
            }
        }else if(requestCode==WRITE_STORAGE_PERMISSION_CODE) {
            if (!(grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                Toast.makeText(requireContext(), "Storage Permission Denied", Toast.LENGTH_SHORT).show()
            }
        }
    }
Low Jung Xuan
  • 25
  • 1
  • 7

1 Answers1

0

onRequestPermissionsResult() method is deprecated in androidx.fragment.app.Fragment.

So you use registerForActivityResult() method instead onRequestPermissionsResult().

Following is kotlin code. .

val permReqLuncher = registerForActivityResult(ActivityResultContracts.RequestPermission()){
  if (it) {
     // Good pass
  } else {
     // Failed pass
  }
}

How to get a permission request in new ActivityResult API (1.3.0-alpha05)?

private ActivityResultLauncher<String> mPermissionResult = registerForActivityResult(
        new ActivityResultContracts.RequestPermission(),
        new ActivityResultCallback<Boolean>() {
            @Override
            public void onActivityResult(Boolean result) {
                if(result) {
                    Log.e(TAG, "onActivityResult: PERMISSION GRANTED");
                } else {
                    Log.e(TAG, "onActivityResult: PERMISSION DENIED");
                }
            }
        });



        // Launch the permission window -- this is in onCreateView()
    floatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         mPermissionResult.launch(Manifest.permission.ACCESS_BACKGROUND_LOCATION);

        }
    });

You can request multiple permissions.

val requestMultiplePermissions = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
    permissions.entries.forEach {
        Log.e("DEBUG", "${it.key} = ${it.value}")
    }
}

requestMultiplePermissions.launch(
    arrayOf(
        Manifest.permission.READ_CONTACTS,
        Manifest.permission.ACCESS_FINE_LOCATION
   )
)
DoctorWho
  • 1,044
  • 11
  • 34