2

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)
Kingg
  • 250
  • 1
  • 12
  • 1
    To be able to help you could you please tell us which line in CustomAdapter.java is line 54 and line 17? – Cyb3rKo Aug 30 '20 at 15:24
  • `holder.imgname.setImageResource((Integer) list_image.get(position));` – Kingg Aug 30 '20 at 15:28
  • while retreving the image it shows error i think image in database was stored in blob – Kingg Aug 30 '20 at 15:29
  • 1
    Have you tried adding the identifier to the ArrayList? Maybe that helps – Cyb3rKo Aug 30 '20 at 15:39
  • i tried bro if i add ` private ArrayList list_name,list_image ;` then it shows error **byte[] cannot be cast to java.lang.String** – Kingg Aug 30 '20 at 15:43
  • i think i want to convert byte to bitmap but i dont know how @Cyb3rKo – Kingg Aug 30 '20 at 15:47
  • Maybe this helps you: https://stackoverflow.com/questions/7620401/how-to-convert-byte-array-to-bitmap – Cyb3rKo Aug 30 '20 at 15:49
  • 1
    Try taking `cursor.getBlob()` into a new byte array and then push it into the ArrayList. – Chaitanya Chavali Aug 30 '20 at 15:49
  • @VSSCHAITANYAChavali sry new to Sndroid Cant know to do ? Where i want to use inside the storeDataInArrays() ? – Kingg Aug 30 '20 at 15:51
  • 1
    @Kingg Yes. Try `byte[] b = cursor.getBlob()` And then `image.add(b)` – Chaitanya Chavali Aug 30 '20 at 15:54
  • i add `byte[] b = cursor.getBlob(2); image.add(b);` and try to run it Shows Error in CustomAdapter in line 17 **byte[] cannot be cast to java.lang.String`**( holder.imgname.setImageResource(Integer.parseInt(list_image.get(position)));)` – Kingg Aug 30 '20 at 15:59

1 Answers1

3

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);