Android:宽度和高度必须为>;0用于转换为位图时的回收器视图
在Sqlite中存储文本和图像,并尝试将这些数据检索到recyclerview中,当将图像显示到recyclerview中时,显示错误宽度和高度必须大于0 在sqlite中保存图像时,我将位图转换为字节 数据库助手Android:宽度和高度必须为>;0用于转换为位图时的回收器视图,android,android-recyclerview,integer,Android,Android Recyclerview,Integer,在Sqlite中存储文本和图像,并尝试将这些数据检索到recyclerview中,当将图像显示到recyclerview中时,显示错误宽度和高度必须大于0 在sqlite中保存图像时,我将位图转换为字节 数据库助手 String query = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(" + COLUMN_ID + &quo
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);
}
}
在onBindViewHolder方法中,您错误地将字节[]转换为整数 您可以使用BitmapFactory.decodeByteArray()执行从字节[]到位图的解码
Bitmap bmp = BitmapFactory.decodeByteArray(list_image. get (position), 0, list_image. get (position). length);
然后使用ImageView.setImageBitmap()
为了能够帮助您,您可以告诉我们CustomAdapter.java中的哪一行是第54行,第17行是holder.imgname.setImageResource((整数)list_image.get(位置))代码>是否尝试将标识符添加到ArrayList?可能是这样的帮助将cursor.getBlob()放入一个新的字节数组,然后将其推入ArrayList。@Kingg是的。尝试
byte[]b=cursor.getBlob()
,然后尝试image.add(b)
谢谢兄弟,i decareImageView image=holder.imgname代码>当我运行应用程序时,它显示错误宽度和高度必须>0@Kingg@Kingg只需尝试image.setImageBitmap(bmp)代码>没有bro,它不起作用,它说需要4个参数,所以我传递holder.imgname.setImageBitmap(Bitmap.createScaledBimat(bmp,0,0,false))
仍然必须是相同的错误宽度和高度>0@Kingg对不起,是打字错误。我再次更新了我的评论检查。谢谢兄弟,但还是同样的错误,我更新了代码Plz检查
<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)
Bitmap bmp = BitmapFactory.decodeByteArray(list_image. get (position), 0, list_image. get (position). length);
image = holder.imgname;
image.setImageBitmap(bmp);