图片存在,但不为null,但是我收到了一个N​​ullPointerException [英] Image exists, not null, but I get an NullPointerException on it

查看:102
本文介绍了图片存在,但不为null,但是我收到了一个N​​ullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短介绍

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

The app I am working on at the moment, takes an image from a JSON, if correct it uses it and saves it as a loading image on next use.

问题

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

All of the sudden, the app started crashing on loading and on further inspection, the file.isFile && file.canRead() check I had in place returns positive but it still crashes on BitmapFactory.decodeStream. The byte count of the file is 8k, compared to other devices I've used, where that same image actually has 43k bytes.

图片json是更大的json消息(大约500kb)的一部分,而不是开头或结尾.在此之前的最后一次使用没有问题,加载或显示下载可能已中途停止的迹象也没有问题.即便如此,该图像仍然具有8kb,所以我想它应该只用它具有的部分创建一些东西,而不是抛出NullPointerException

The image json is part of a much larger json message (approx 500kb) and it's not at the start or the end. The last use before this happened there was no problem loading or sign showing that download might have stopped half way. Even so, the image still has 8kb so I imagine it should just create something with the part it has rather then throwing NullPointerException

我正在使用Android Studio 3.0 Beta 4,代码在Kotlin中.我必须已经在此设备上运行了100次,即使发生了这种情况,它也可以在其他测试设备上完美运行,再加上在启动时会获得完全相同的json的iOS应用

I am using Android Studio 3.0 Beta 4 and code is in Kotlin. I must have run it on this device 100 times, and even after this occurred it runs perfectly fine on other test devices, plus the iOS app that gets the exact same json at start up

我在寻找什么

提供有关如何解决此问题的建议,或者提供有关是Android还是Kotlin中已知的错误的信息.我可能可以在设备上重新安装该应用程序,并且可以再次正常运行,但是如果可以阻止,则不希望在应用程序发布后发生这种情况.

Advice on how to deal with the issue, or information whether it's a known bug in Android or Kotlin. I can probably reinstall the app on the device and it will be ok again but would not want this happening once the app was released, if I can prevent it.

LaterEdit :根据Xaviers的建议,我已经将文件复制到了桌面上,并且肯定是格式错误的,加载时实际上显示了Mac上运行的某些格式严重错误的图像.我尝试上传它,但是它只是显示为空白图像.它仍然有8kb,因此它包含一些数据. 这怎么可能并且可以预防?

LaterEdit: With Xaviers suggestion, I have copied the file onto the desktop and it's definitely malformed, when it loads it actually shows some seriously malformed image of things running on the Mac. I have tried to upload it but it just displays as an empty white image. It still has 8kb, so it contains some data. How is this possible and can it be prevented?

代码

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

Loading up the app, I check if image exists and load it into an 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

推荐答案

您确定崩溃是在同一loadImage()方法中,而不是在另一方法中吗?在错误日志中,它表示崩溃是在BitmapFactory.decodeReso…ystem(), R.drawable.logo)中,这似乎是另一回事.

Are you sure the crash is in the same loadImage() method and not on another one? In the error log it says that the crash is in BitmapFactory.decodeReso…ystem(), R.drawable.logo), which seems a different one.

此外,您是否尝试过先进行重建?我已经看到AS 3.0 Beta中的许多问题可以通过解决这些问题来解决.

Also, have you tried to do a Rebuild first? I've seen many problems in AS 3.0 betas that are resolved by doing that.

来自文档 ,即使fis为null,它也不会因该错误而崩溃,只需返回null(强调是我的).

From documentation, even if fis is null it should not crash with this error, just return a null (emphasis is mine).

将输入流解码为位图. 如果输入流为空,或者无法用于解码位图,则该函数返回null.流的位置将为读取编码数据后的位置.

Decode an input stream into a bitmap. If the input stream is null, or cannot be used to decode a bitmap, the function returns null. The stream's position will be where ever it was after the encoded data was read.

只是丢弃东西,您可以为BitmapFactory添加import语句吗?应该是import android.graphics.BitmapFactory.

Just to discard things, can you add the import statement for BitmapFactory? Should be import android.graphics.BitmapFactory.

这篇关于图片存在,但不为null,但是我收到了一个N​​ullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆