Kingg
Kingg

Reputation: 270

Android : width and height must be > 0 for recycler view when Converting to Bitmap

Stored text and images in Sqlite and trying to retreive the those data's into the recyclerview, and when dispaying the Images into recyclerview it Shows Error width and height must be > 0

while saving the image in sqlite i convert bitmap to byte

databasehelper

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"
        />

logcat

 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)

Upvotes: 2

Views: 660

Answers (1)

Chaitanya Chavali
Chaitanya Chavali

Reputation: 853

In the onBindViewHolder method, you are wrongly converting byte[] to Integer.

You can use BitmapFactory.decodeByteArray() to perform the decoding from byte[] to bitmap.

Bitmap bmp = BitmapFactory.decodeByteArray(list_image. get (position), 0, list_image. get (position). length);  

Then use ImageView.setImageBitmap()

image = holder.imgname;
image.setImageBitmap(bmp);

Upvotes: 3

Related Questions