android - 如何从 sqlite db 检索转换为字节数组的图像并将其放置在 RecyclerView 中?
问题描述
我陷入了困境。这是我的第一个 android 应用程序之一,我不知道如何克服这个问题。我的问题是我有一个 SQLite 数据库,我在其中保存转换为字节数组的图像和更多数据,但是当我检索该数据并希望在 RecyclerView 中显示时,图像没有显示在 RecyclerView 列表中。其他数据正常显示,但图像字段为空。有谁知道我的代码有什么问题?
这就是我试图在适配器(onBindViewHolder)中设置列表项的方式:
byte[] image = selectedGame.getImage();
Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
gameItemView.imageGame.setImageBitmap(bitmap);
这是我的 GameListActivity,我用 gamesData 填充列表:
gamesRecyclerView = findViewById(R.id.gamesRecyclerView);
gamesData = new ArrayList<>();
gamesAdapter = new GamesAdapter(gamesData);
((GamesAdapter) gamesAdapter).setGamesActivity(this);
Cursor cursor = db.getGames();
gamesData.clear();
if(cursor.getCount() == 0){
Toast.makeText(getApplicationContext(), "No games in database", Toast.LENGTH_SHORT).show();
}
else {
while (cursor.moveToNext()) {
gamesData.add(new Game(cursor.getInt(cursor.getColumnIndex("Id")),
cursor.getString(cursor.getColumnIndex("gameTitle")),
cursor.getString(cursor.getColumnIndex("gameGenre")),
cursor.getInt(cursor.getColumnIndex("year")),
cursor.getString(cursor.getColumnIndex("gameDescription")),
cursor.getInt(cursor.getColumnIndex("gamePC")),
cursor.getInt(cursor.getColumnIndex("gamePS3")),
cursor.getInt(cursor.getColumnIndex("gamePS4")),
cursor.getInt(cursor.getColumnIndex("gameXONE")),
cursor.getInt(cursor.getColumnIndex("gameX360")),
cursor.getInt(cursor.getColumnIndex("gameNINTENDO")),
cursor.getInt(cursor.getColumnIndex("likesCount")),
cursor.getBlob(cursor.getColumnIndex("image"))
));
}
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
gamesRecyclerView.setAdapter(gamesAdapter);
gamesRecyclerView.setLayoutManager(layoutManager);
在这里,我正在向数据库添加一个游戏(有趣的是,当我在添加一些游戏后尝试获取所有游戏时,它会打印出错误的字节数组):
btnAddYourGame.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
try{
byte[] gameImage = imageViewToByte(image);
System.out.println(gameImage.length);
Boolean checkGame = openHelper.checkIfGameExists(title);
if (checkGame == true) {
openHelper.addGame(title, genre, year, description, pcAvailable, ps3Available, ps4Available, x360Available, xoneAvailable, nintendoAvailable, gameImage);
Cursor cursor = openHelper.getGames();
while (cursor.moveToNext()) {
System.out.println(cursor.getBlob(cursor.getColumnIndex("image")).length);
}
Toast.makeText(getApplicationContext(), getResources().getString(R.string.action_game_add_success), Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), getResources().getString(R.string.action_game_add_failed), Toast.LENGTH_SHORT).show();
}
}
}
catch (Exception e){
e.printStackTrace();
}
}
});
解决方案
推荐阅读
- html - 为什么这个 Bootstrap 4 Navbar 不能创建等宽的菜单项?
- docker - gRPC 流式处理过程返回“方法未实现”。在 Azure 容器实例中运行时
- r - R Shiny - 自定义选项不适用于tippy::with_tippy()
- xodus - 如何使用 Xodus DNQ 检查组合唯一性
- ios - 从 SwiftUI 上的部分列表中删除项目
- single-sign-on - SAML 属性经常实现 eduPerson 规范?有没有更通用的规范?
- anaconda - 内核开发版本与centos7上的内核不同
- c# - 事件存储连接已关闭
- cloud-foundry - Cloud Foundry:在 buildpack 之前运行自定义命令
- c# - 客户端机器上的应用程序,直接从服务器调用数据库操作