【问题标题】:Save image in SQLite database getting from the webservice将图像保存在从 Web 服务获取的 SQLite 数据库中
【发布时间】:2013-03-05 21:09:41
【问题描述】:

我正在开发 android 应用程序,我在其中解析来自 web 服务的数据,然后将其存储在数据库中。在这里,我还想将图像存储在数据库中并在下一个活动中检索。

我正在使用此代码将图像插入数据库。

Bitmap yourSelectedImage;
ByteArrayOutputStream stream;
byte[] byteArray;

这是在for循环中

{ 
yourSelectedImage = BitmapFactory.decodeFile(SingleImageURL[i]);
stream = new ByteArrayOutputStream();
yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100,
                    stream);
byteArray = stream.toByteArray();

database.insertDetail(ID[i],byteArray[i]); 

}

这里 SingleImageURL 有来自网络服务的图像 url。

当我运行代码时,它会在这一行给出错误 nullPointerException

 yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100,
                    stream);

请告诉我哪里出错了。以及如何做到这一点。

【问题讨论】:

  • 调试你的程序,看看你在 SingleImageURL 中得到了什么??

标签: android parsing android-sqlite


【解决方案1】:

使用this link 加载您的位图yourSelectedImage。然后通过将其转换为byte[] 将其保存到数据库中

您正在解码一个文件,它实际上是一个 URL。所以你的yourSelectedImage 是null。当您尝试使用

压缩它时
yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream);

你会得到NullPointerException

【讨论】:

    猜你喜欢
    • 2018-10-03
    • 2013-07-01
    • 1970-01-01
    • 2016-06-25
    • 2012-11-29
    • 1970-01-01
    • 2020-02-10
    • 2013-10-04
    • 1970-01-01
    相关资源
    最近更新 更多