首页 > 解决方案 > 如何从 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();
                }

            }
        });

标签: androidimagesqliteandroid-recyclerview

解决方案


推荐阅读