android - 图像存在,不为空,但我得到一个 NullPointerException

标签 android kotlin bitmapfactory

简介

我目前正在开发的应用程序从 JSON 中获取图像,如果正确,它会使用它并在下次使用时将其保存为加载图像。

问题

突然,应用程序开始在加载和进一步检查时崩溃,我进行的 file.isFile && file.canRead() 检查返回正数,但它仍然在 BitmapFactory.decodeStream。 与我使用的其他设备相比,该文件的字节数为 8k,其中同一图像实际上有 43k 字节。

图像 json 是更大的 json 消息(大约 500kb)的一部分,它不在开头或结尾。发生这种情况之前的最后一次使用没有加载问题或标志显示下载可能已停止一半。即便如此,图像仍然有 8kb 所以我想它应该只用它的部分创建一些东西而不是抛出 NullPointerException

我使用的是 Android Studio 3.0 Beta 4,代码使用 Kotlin。我必须在这个设备上运行它 100 次,即使在这个发生之后它在其他测试设备上运行得很好,加上在启动时获得完全相同的 json 的 iOS 应用程序

我在找什么

关于如何处理该问题的建议,或者它是否是 Android 或 Kotlin 中的已知错误的信息。我可能可以在设备上重新安装该应用程序,它会再次正常,但我不希望在应用程序发布后发生这种情况,如果我能阻止的话。

LaterEdit:根据 Xaviers 的建议,我已将该文件复制到桌面上,但它肯定是格式错误的,当它加载时,它实际上显示了一些在 Mac 上运行的东西的严重格式错误的图像。我试图上传它,但它只显示为空白的白色图像。它仍然有 8kb,所以它包含一些数据。 这怎么可能,如何预防?

代码

加载应用程序,我检查图像是否存在并将其加载到 imageView 中

try {
     if (DesignSupport().imageExists("logo", this)) {
         if (DesignSupport().loadImage("logo", this) != null) {
             imageView.setImageBitmap(DesignSupport().loadImage("logo", this))
         }
     }
 } catch (error: Error) {
     Log.i(tag, error.stackTrace.toString())
 }

检查图片是否存在

fun imageExists(string: String, context: Context) : Boolean {
        val path = android.os.Environment.getExternalStorageDirectory().toString() + "/" + context.applicationInfo.name + "/" + string + ".png"
        val file = File(path)
        return (file.isFile && file.canRead())
    }

加载图片

    fun loadImage(string: String, context: Context) : Bitmap {
            val path = android.os.Environment.getExternalStorageDirectory().toString() + "/" + context.applicationInfo.name + "/" + string + ".png"
            val fis = FileInputStream(path)
// it crashes at the next line, but when I print fis.available() it returns 8200
            return BitmapFactory.decodeStream(fis)
        }

崩溃日志

 java.lang.RuntimeException: Unable to start activity ComponentInfo{.GeneralAccessScreens.startupScreen}: java.lang.IllegalStateException: BitmapFactory.decodeStream(fis) must not be null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2412)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470)
at android.app.ActivityThread.access$900(ActivityThread.java:174)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5593)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: BitmapFactory.decodeStream(fis) must not be null
at SupportMethods.DesignSupport.loadImage(DesignSupport.kt:45)
at .GeneralAccessScreens.startupScreen.onCreate(startupScreen.kt:34)
at android.app.Activity.performCreate(Activity.java:5458)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)

导入语句

import android.app.Activity
import android.content.Context
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.Base64
import android.util.Log
import android.view.inputmethod.InputMethodManager
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream

最佳答案

抛出特定异常是因为 BitmapFactory.decodeStream(fis) 返回 null 而指定函数返回一个不可为 null 的对象。使返回值可为 null 或定义适当的替换(如果返回 null)。

在后台 Kotlin 生成以下“java”代码:

Bitmap tmp = BitmapFactory.decodeStream(fis);
Intrinsics.checkExpressionValueIsNotNull(tmp, "BitmapFactory.decodeStream(fis)");
return tmp;

异常的来源是 Intrinsics.checkExpressionValueIsNotNull 调用。

关于android - 图像存在,不为空,但我得到一个 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46040354/

相关文章:

android:从MultiAutoComplete Textview中获取所选项目并进行更新并更新EditView?

kotlin - 我自己对 Kotlin 的 try-with-resources 缺席的解决方案

java - Kotlin - 无法访问元素 [Ljava.lang.Object

java - ImageIO.read() 和 BitmapFactory.decodeByteArray()

java - BitmapFactory 打开失败 : ENOENT (No such file or directory)

android - 导航到另一个 Fragment 时,Fragment 中的 RecyclerView 崩溃

android - 一个典型的 Android 应用程序使用多少内存是合理的

java - Android Wear 与可穿戴设备的通信无法正常工作

android - 在 Kotlin 的 Stateflow 上使用 map

android - 将许多大位图图像加载为缩略图 - Android