gpt4 book ai didi

android - 将图像保存在从网络服务获取的 SQLite 数据库中

转载 作者:搜寻专家 更新时间:2023-11-01 09:00:40 24 4
gpt4 key购买 nike

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

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

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);

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

最佳答案

使用 this link 加载位图 yourSelectedImage .然后通过将其转换为 byte[]

将其保存到数据库

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

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

你得到 NullPointerException

关于android - 将图像保存在从网络服务获取的 SQLite 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15224397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com