java - 图像中的错误 : incompatible types: byte[] cannot be converted to int.

标签 java android sqlite blob

我尝试在 ImageView 中显示图像。类型是 BLOB 到 SQLite 数据库中,但我发现 android studio 中的错误是 Error:(43, 220) 错误:不兼容的类型: byte[] 无法转换为 int 。

这是我在数据库类中的代码

public class Database {
public static final String DATABASE_NAME = "dic.sqlite";
public static final String TABLE_FIRST = "en1_word";
public static final String TABLE_SECOND = "en2_word";
public static final String COLUMN_ID = "id";
public static final String COLUMN_WORD = "word";
public static final String COLUMN_MEAN = "mean";
public static final String COLUMN_CATEGORY = "category";
public static final String COLUMN_PIC = "pic";
public static final String COLUMN_FAVORITE = "favorite";}

和databasedao类

public class DictionaryDaoImpl implements DictionaryDao {
private SQLiteDatabase sqLiteDatabase;
public DictionaryDaoImpl(SQLiteDatabase sqLiteDatabase){
    this.sqLiteDatabase = sqLiteDatabase;

}
@Override
public Word getWordById(int id) {
    Cursor cursor = sqLiteDatabase.query(Util.getCurrentTableName(),new String[]{Database.COLUMN_ID, Database.COLUMN_WORD, Database.COLUMN_MEAN, Database.COLUMN_FAVORITE,Database.COLUMN_CATEGORY ,Database.COLUMN_PIC},
            Database.COLUMN_ID + " = ?",new String[]{String.valueOf(id)},null,null,null,null);

    int idColumnIndex = cursor.getColumnIndex(Database.COLUMN_ID);
    int wordColumnIndex = cursor.getColumnIndex(Database.COLUMN_WORD);
    int meanColumnIndex = cursor.getColumnIndex(Database.COLUMN_MEAN);
    int favoriteColumnIndex = cursor.getColumnIndex(Database.COLUMN_FAVORITE);
    int categoryColumnIndex = cursor.getColumnIndex(Database.COLUMN_CATEGORY);
    byte[] picColumnIndex = cursor.getBlob(cursor.getColumnIndex(Database.COLUMN_PIC));

    Word word = null;
    if (cursor.moveToNext()){
        word = new Word(cursor.getInt(idColumnIndex),cursor.getString(wordColumnIndex), cursor.getString(meanColumnIndex), cursor.getInt(favoriteColumnIndex)==1,cursor.getString(categoryColumnIndex), cursor.getBlob(picColumnIndex));
    }
    return word;
}

和词类

public class Word {
private int id;
private String word;
private String mean;
boolean isFavorite;
private byte[] pic;
private String category;

public Word(int id, String word, String mean, boolean isFavorite, byte[] pic, String category) {
    this.id = id;
    this.word = word;
    this.mean = mean;
    this.isFavorite = isFavorite;
    this.pic = pic;
    this.category = category;
}
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getWord() {
    return word;
}
public void setWord(String word) {
    this.word = word;
}
public String getMean() {
    return mean;
}
public void setMean(String mean) {
    this.mean = mean;
}
public boolean isFavorite() {
    return isFavorite;
}
public void setIsFavorite(boolean isFavorite) {
    this.isFavorite = isFavorite;
}
public String getCategory() {
    return category;
}
public void setCategory(String category) {
    this.category = category;
}
public byte[] getPic() {
    return pic;
}
public void setPic(byte[] pic) {
    this.pic = pic;
}

最佳答案

在此 cursor.getBlob(picColumnIndex) 中,picColumnIndex 应为 int 类型,而您已将其定义为 byte[ ] picColumnIndex

而不是 byte[] picColumnIndex = curve.getBlob(cursor.getColumnIndex(Database.COLUMN_PIC)); 我相信您想要 int picColumnIndex = curve.getColumnIndex(Database.COLUMN_PIC);

关于java - 图像中的错误 : incompatible types: byte[] cannot be converted to int.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36208401/

相关文章:

sql - 如何对整个表的多列进行RANK?

scala - 带有 Slick and Play 的 SQLite

java - 复杂性 良好、中等和最差

java - 如何模拟传出的套接字连接?

Android Studio Unknown Emulator 正在运行且无法终止

Android:恢复带有动画 View 的 Activity 后,如何启动应用于 ImageView 的无限动画?

SQLite 选择计数 10 小时前的记录

java - 让 ValueListener 只发生一次

java - 如何使用变量声明新的 JComboBox

Android Ad Whirl AdMob + InMobi 奇怪的行为