Kotlin - 在 JAVA 上从相机捕获图像时出错,代码正常

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

这是我的 JAVA 代码转换为 Kotlin。 转换后代码给了我一些错误,在 java 上它完美无误地工作。 从相机拍摄图像后抛出错误。 是什么导致了这个问题??

我正在使用 'com.github.yalantis:ucrop:2.2.1-native' 库来裁剪图像

    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)
}

这是错误信息,

 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.access00(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)
java.lang.IllegalArgumentException: Parameter specified as non-null is null:

很难说哪一个是空的。例如

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

Intent 可以为 null,应将其标记为 nullabe。将其更改为

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