在 Sqlite 中存储文本和图像,并尝试将这些数据检索到 recyclerview 中,当将图像显示到 recyclerview 中时,它显示错误宽度和高度必须 > 0
在 sqlite 中保存图像时,我将位图转换为字节
数据库助手
String query =
"CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY ,"
+ COLUMN_TITLE + " TEXT, "
+ COLUMN_IMAGE + " BLOB )";
sqLiteDatabase.execSQL(query);
CustumAdapter.java
private LayoutInflater mInflater;
private ArrayList<String> list_name;
ArrayList<byte[]> list_image ;
public CustomAdapter( Context context
, ArrayList list_name, ArrayList list_image ){
mInflater = LayoutInflater.from(context);
this.list_name = list_name;
this.list_image =list_image ;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.listname.setText(String.valueOf(list_name.get(position)));
Bitmap bmp = BitmapFactory.decodeByteArray(list_image. get (position), 0, list_image. get (position).length);
ImageView image = holder.imgname;
image.setImageBitmap(Bitmap.createScaledBitmap(bmp, image.getWidth(), image.getHeight(), false));
}
@Override
public int getItemCount() {
return list_name.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView listname;
ImageView imgname;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
listname = itemView.findViewById(R.id.list_name);
imgname = itemView.findViewById(R.id.image_list);
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
DatabaseHelper myDB;
ArrayList<String> title;
ArrayList<byte[]> image;
CustomAdapter ca;
myDB = new DatabaseHelper(MainActivity.this);
title = new ArrayList<>();
image = new ArrayList<byte[]>();
storeDataInArrays();
ca = new CustomAdapter(MainActivity.this, title,image);
recyclerView.setAdapter(ca);
void storeDataInArrays(){
Cursor cursor = myDB.readAllData();
if(cursor.getCount() == 0){
Toast.makeText(this, "No Data.", Toast.LENGTH_SHORT).show();
}else{
while (cursor.moveToNext()){
title.add(cursor.getString(1));
image.add(cursor.getBlob(2));
}
}
}
RecyclerView.xml
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:layout_marginBottom="50dp"
/>
日志猫
java.lang.IllegalArgumentException: width and height must be > 0
at android.graphics.Bitmap.createBitmap(Bitmap.java:1055)
at android.graphics.Bitmap.createBitmap(Bitmap.java:1022)
at android.graphics.Bitmap.createBitmap(Bitmap.java:972)
at android.graphics.Bitmap.createBitmap(Bitmap.java:892)
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:763)
at com.Karthickyuvan.check.CustomAdapter.onBindViewHolder(CustomAdapter.java:61)
at com.Karthickyuvan.check.CustomAdapter.onBindViewHolder(CustomAdapter.java:19)
最佳答案
在 onBindViewHolder 方法中,您错误地将 byte[] 转换为 Integer。
您可以使用 BitmapFactory.decodeByteArray() 执行从 byte[] 到位图的解码。
Bitmap bmp = BitmapFactory.decodeByteArray(list_image. get (position), 0, list_image. get (position). length);
然后使用ImageView.setImageBitmap()
image = holder.imgname;
image.setImageBitmap(bmp);
关于安卓 : width and height must be > 0 for recycler view when Converting to Bitmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63658886/