Akhil T Mohan
Akhil T Mohan

Reputation: 313

Kotlin - Error Capturing Image From camera on JAVA code its OK

This is my JAVA Code Converted to Kotlin. after conversion the code gives me some error, on java its working perfectly without any errors. After taking image from camera the error is thrown . what causes the issue ??

I am using 'com.github.yalantis:ucrop:2.2.1-native' library for cropping the image

    private fun dispatchTakePictureIntent() {
    val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
    if (takePictureIntent.resolveActivity(baseContext.packageManager) != null) {
        var photoFile: File? = null
        try {
            photoFile = createImageFile()
        } catch (ex: IOException) {
            // Error occurred while creating the File
        }

        if (photoFile != null) {
            try {
                photoURI = FileProvider.getUriForFile(baseContext,
                        "com.anvinsolutions.akhil.pravda.fileprovider",
                        photoFile)
                //Toast.makeText(getBaseContext(), photoURI.getPath(), Toast.LENGTH_SHORT).show();
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO)
            } catch (e: Exception) {
                e.printStackTrace()
            }

        }
    }
}
    private fun dispatchTakePictureIntent() {
    val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
    if (takePictureIntent.resolveActivity(baseContext.packageManager) != null) {
        var photoFile: File? = null
        try {
            photoFile = createImageFile()
        } catch (ex: IOException) {
            // Error occurred while creating the File
        }

        if (photoFile != null) {
            try {
                photoURI = FileProvider.getUriForFile(baseContext,
                        "com.anvinsolutions.akhil.pravda.fileprovider",
                        photoFile)
                //Toast.makeText(getBaseContext(), photoURI.getPath(), Toast.LENGTH_SHORT).show();
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO)
            } catch (e: Exception) {
                e.printStackTrace()
            }

        }
    }
}

@Throws(IOException::class)
private fun createImageFile(): File {
    // Create an image file name
    val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
    //Toast.makeText(getBaseContext(), timeStamp, Toast.LENGTH_SHORT).show();
    val imageFileName = "JPEG_$timeStamp"
    val storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
    val image = File.createTempFile(
            imageFileName, /* prefix */
            ".jpg", /* suffix */
            storageDir      /* directory */
    )

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.absolutePath
    Log.e("PATH ==> ",mCurrentPhotoPath)
    return image
}

/*********************************** CAMERA  */


private fun startCropActivity(uri: Uri) {
    //Log.e("","wfwefwefwef")
    var destinationFileName = "Akhil"
    destinationFileName += ".jpg"
    val uCrop = UCrop.of(uri, Uri.fromFile(File(cacheDir, destinationFileName)))
    val options = UCrop.Options()
    //options.withAspectRatio(3,4);
    options.setFreeStyleCropEnabled(true)
    //options.withMaxResultSize(396,512);
    options.setHideBottomControls(true)
    options.setCompressionFormat(Bitmap.CompressFormat.PNG)
    uCrop.withOptions(options)
    uCrop.start(this@member_form)
}

private fun pickFromGallery() {
    val intent = Intent()
    intent.type = "image/*"
    intent.action = Intent.ACTION_GET_CONTENT
    intent.addCategory(Intent.CATEGORY_OPENABLE)
    startActivityForResult(Intent.createChooser(intent, "Select one"), REQUEST_SELECT_PICTURE)
}


private fun handleCropResult(result: Intent) {
    croppedImage = UCrop.getOutput(result)!!
    if (croppedImage != null) {
        //Toast.makeText(getBaseContext(), "OK CROP COMPLETED", Toast.LENGTH_SHORT).show();
        take_photo.setImageResource(android.R.color.transparent)
        take_photo.setImageURI(croppedImage)
        /*ViewGroup.LayoutParams layoutParams = take_photo.getLayoutParams();
        layoutParams.width = 396;
        layoutParams.height = 512;
        take_photo.setLayoutParams(layoutParams);*/
        take_photo.scaleType = ImageView.ScaleType.CENTER
        take_photo.adjustViewBounds = true
        //ResultActivity.startWithUri(SampleActivity.this, croppedImage);
    } else {
        Toast.makeText(baseContext, "Error", Toast.LENGTH_SHORT).show()
    }
}

private fun handleCropError(result: Intent) {
    val cropError = UCrop.getError(result)
    if (cropError != null) {
        Log.e("Error: ", "handleCropError: ", cropError)
        Toast.makeText(baseContext, cropError.message, Toast.LENGTH_LONG).show()
    } else {
        Toast.makeText(baseContext, "Unexpected Error!", Toast.LENGTH_SHORT).show()
    }
}


public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
    Log.e("Result", "RequestCode=$requestCode Result Code=$resultCode")

    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
        try {
            startCropActivity(photoURI!!)
            Log.e("","startCropActivity")
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }else{
        Log.e("Result"," : NOT OK")
    }

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == REQUEST_SELECT_PICTURE) {
            val selectedUri = data.data
            if (selectedUri != null) {
                startCropActivity(data.data!!)
            } else {
                Toast.makeText(baseContext, "Error", Toast.LENGTH_SHORT).show()
            }
        } else if (requestCode == UCrop.REQUEST_CROP) {
            handleCropResult(data)
        }
    }
    if (resultCode == UCrop.RESULT_ERROR) {
        handleCropError(data)
        Log.e("", "CROP RESULT ERROR")
    }
    super.onActivityResult(requestCode, resultCode, null)
}

This is the error message,

 Process: com.anvinsolutions.akhil.pravda, PID: 14120
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.anvinsolutions.akhil.pravda/com.anvinsolutions.akhil.pravda.member_form}: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter data
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3738)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3781)
    at android.app.ActivityThread.access$1500(ActivityThread.java:153)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1424)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:5523)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
 Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter data
    at com.anvinsolutions.akhil.pravda.member_form.onActivityResult(member_form.kt)
    at android.app.Activity.dispatchActivityResult(Activity.java:6508)

Upvotes: 0

Views: 1106

Answers (1)

Blackbelt
Blackbelt

Reputation: 157457

java.lang.IllegalArgumentException: Parameter specified as non-null is null:

hardly to say which one exactly is null. For instance

public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {

the Intent could be null, and it should be marked as nullabe. Change it with

public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {

Upvotes: 3

Related Questions