简介
我目前正在使用的应用程序会从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
最佳答案
您确定崩溃是在相同的loadImage()
方法中而不是在另一个方法中吗?在错误日志中,它表示崩溃是在BitmapFactory.decodeReso…ystem(), R.drawable.logo)
中,这似乎是另外一种情况。
另外,您是否尝试过先进行重建?我已经看到AS 3.0 Beta中的许多问题可以通过解决这些问题来解决。
从documentation,即使fis
为null,它也不会因该错误而崩溃,只需返回null
(重点是我的)。
只是为了丢东西,您可以为import
添加BitmapFactory
语句吗?应该是import android.graphics.BitmapFactory
。