【问题标题】:Getting BLOB data in Android, null exception在Android中获取BLOB数据,空异常
【发布时间】:2012-04-04 11:06:54
【问题描述】:

我在 Sqlite 的 BLOB 列中插入了一个位图数据(转换后的字节 [] 数组)。
我使用这个来源来获取数据。

//Get the data
byte[] byteArray = helper.getPicture(appWidgetId);
//Change byte[] data to Bitmap
Bitmap bmPicture = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

但是android显示错误信息,它的位图是空的
所以我添加了检查源。

//Get the data
byte[] byteArray = helper.getPicture(appWidgetId);
//Check, If byte is not null
if (byteArray != null) {
//Change it to Bitmap
    Bitmap bmPicture = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
}

这是完美的......
我不明白为什么添加“if source”可以加载数据。
为什么不添加'if source' can't load data.

它看起来像界面...:

【问题讨论】:

  • 是你的helper.getPicture(appWidgetId);异步调用?如果不是,if 语句应该没有区别。

标签: java android sqlite null blob


【解决方案1】:

添加一些日志记录并检查您使用 if 阻止的情况是否/何时发生。

if (byteArray != null) {
    //Change it to Bitmap
    Bitmap bmPicture = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    Log.d("TAG", "it worked :)");
} else {
    Log.d("TAG", "it's null :(");
}

【讨论】:

    【解决方案2】:

    我认为您必须调试您的应用程序。在正确的位置设置一些断点并继续。这是在运行时计算变量值的最简单方法。我认为有问题:

    byte[] byteArray = helper.getPicture(appWidgetId);
    

    这种方法有时显然不会像预期的那样起作用,所以深入研究一下。

    if 语句不会使错误行为消失,它只会在getPicture 方法返回有效结果时向您显示成功。所以在 null 结果的情况下,你什么也看不到,对吧?

    另一个建议使用异常来确定何时出现问题,这样您就可以对意外行为做出反应;-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-27
      • 2020-02-14
      • 1970-01-01
      • 2011-04-10
      • 1970-01-01
      • 2012-07-19
      相关资源
      最近更新 更多