Android:宽度和高度必须>转换为位图时,对于回收者视图为0 [英] Android : width and height must be > 0 for recycler view when Converting to Bitmap

查看:86
本文介绍了Android:宽度和高度必须>转换为位图时,对于回收者视图为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Sqlite中存储文本和图像,并尝试将这些数据检索到recyclerview中,并且在将图像支付到recyclerview中时显示错误宽度和高度必须> 0

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

将图像保存在sqlite中时,我将位图转换为字节

while saving the image in sqlite i convert bitmap to byte

数据库助手

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

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

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

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)

推荐答案

在onBindViewHolder方法中,您错误地将byte []转换为Integer.

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

您可以使用BitmapFactory.decodeByteArray()来执行从byte []到位图的解码.

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

然后使用ImageView.setImageBitmap()

Then use ImageView.setImageBitmap()

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

这篇关于Android:宽度和高度必须&gt;转换为位图时,对于回收者视图为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆