简体   繁体   English

kotlin.TypeCastException:null无法强制转换为非null类型android.graphics.Bitmap

[英]kotlin.TypeCastException: null cannot be cast to non-null type android.graphics.Bitmap

I'm making an adaptation so that instead of opening the camera take an image of the gallery, but I'm having this error 我正在进行改编,以便不用打开相机拍摄画廊的图像,但我有这个错误

SOLVED ---------------------------------------------------------------------------------------------------------------> error at line val bitmap = MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap 解决了 - - - - - - - - - - - - - - - - - - - - - - - - - -------------------------------------------------- ------------>错误在第val位bitmap = MediaStore.Images.Media.getBitmap(context.contentResolver,this.imageUri)作为位图

log 日志

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: map.app, PID: 7359
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=android:fragment:3, request=1000, result=-1, data=Intent { dat=content://com.google.android.apps.photos.contentprovider/-1/1/content://media/external/images/media/48/ORIGINAL/NONE/1141690198 flg=0x1 clip={text/uri-list U:content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F48/ORIGINAL/NONE/1141690198} }} to activity {map.app/map.app.MainActivity}: kotlin.TypeCastException: null cannot be cast to non-null type android.graphics.Bitmap
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4268)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4312)
        at android.app.ActivityThread.-wrap19(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1644)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: kotlin.TypeCastException: null cannot be cast to non-null type android.graphics.Bitmap
        at map.app.fragments.ReportFragment.onActivityResult(ReportFragment.kt:272)
        at android.app.Activity.dispatchActivityResult(Activity.java:7303)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4264)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4312) 
        at android.app.ActivityThread.-wrap19(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1644) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6494) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
Application terminated.

code

fun openCamera() {
        try {
            val imageFile = createImageFile()
            val callCameraIntent =  Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            if(callCameraIntent.resolveActivity(activity.packageManager) != null) {
                val authorities = activity.packageName + ".fileprovider"
                this.imageUri = FileProvider.getUriForFile(context, authorities, imageFile)
                callCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri)
                startActivityForResult(callCameraIntent, IMAGE_PICK_CODE)
            }
        } catch (e: IOException) {
            Toast.makeText(context, "Could not create file!", Toast.LENGTH_SHORT).show()
        }
    }

part error code line error at line val bitmap = MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap Caused by: kotlin.TypeCastException: null cannot be cast to non-null type android.graphics.Bitmap 部分错误代码行错误在第val行bitmap = MediaStore.Images.Media.getBitmap(context.contentResolver,this.imageUri)as Bitmap引起:kotlin.TypeCastException:null无法转换为非null类型android.graphics.Bitmap

  override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == IMAGE_PICK_CODE && resultCode == RESULT_OK) {


            try {

                //Getting the Bitmap from Gallery
                val bitmap = MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap
                this.imgThumb!!.setImageBitmap(bitmap)
                this.pictureTaken = true
            } catch (e:IOException) {
                e.printStackTrace()
            }
        } else {
            Toast.makeText(context, "Error loading image", Toast.LENGTH_LONG)
        }
    }

    @Throws(IOException::class)
    fun createImageFile(): File {
        val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
        val imageFileName: String = "JPEG_" + timeStamp + "_"
        val storageDir: File = activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
        if(!storageDir.exists()) storageDir.mkdirs()
        val imageFile = File.createTempFile(imageFileName, ".jpg", storageDir)
        imageFilePath = imageFile.absolutePath
        return imageFile
    }

You must cast it to a nullable type instead of non nullable. 您必须将其强制转换为可空类型而不是非可空类型。 Do the following: 请执行下列操作:

val bitmap = MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap?

Note that the Bitmap? 注意Bitmap? instead of Bitmap 而不是Bitmap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Kotlin(null 不能强制转换为非 null 类型)自定义日历实现 - Kotlin (null cannot be cast to non-null type) Custom Calendar Implementation android通过kotlin返回非空字体 - android return non-null typeface by kotlin 在我的应用程序中,不能将null强制转换为非null类型的java.util.HashMap - null cannot be cast to non-null type java.util.HashMap in my application 尝试在空对象引用上调用虚拟方法'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' - Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference 非空对象类型上的 Retrofit-Kotlin NullPointerException - Retrofit-Kotlin NullPointerException on non-null object type 测试 Kotlin 中的非空参数 - Testing a non-null parameter in Kotlin Jackson ObjectMapper冲突的setter定义(Android.Graphics.Bitmap) - Jackson ObjectMapper Conflicting setter definitions (Android.Graphics.Bitmap) 为什么mvn pact:publish抛出NoClassDefFoundError kotlin.TypeCastException - Why mvn pact:publish throws NoClassDefFoundError kotlin.TypeCastException 在没有Android的情况下从Java使用android.graphics.Bitmap - Use android.graphics.Bitmap from Java without Android 从C ++创建android.graphics.Bitmap - Creating an android.graphics.Bitmap from C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM