I am trying to implement swipe to delete in RecyclerView. Everything seems to be working fine except drawing a delete icon below the item that\'s being swiped.
Thi
I don't understand why it;s null since I've already assigned it a value.
Yes. You did => null
. The problem is elsewhere. See docs for decodeResource():
Returns: The decoded bitmap, or null if the image could not be decoded.
so you need to a) always check for that condition, b) check why exactly it happens with the data you try to decode.
The error is because of the drawable might be a Vector.
Drawable drawable = ContextCompat.getDrawable(StreamActivity.this,R.drawable.ic_close)
Bitmap icon = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(icon);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
I switched from:
android:layout_width="1095dp"
android:layout_height="1200dp"
To:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Then the error went away.
Trying to decode a vector drawable into Bitmap, gives such error. So try to use any image with format .png, .jpeg etc in case of decoding resource to Bitmap.
My problem was that android added a vector drawable anydpi
along with the multiple created image assets for the icon. I deleted the anydpi
variant and it all works fine now.
Answer is check if bitmap is null or reycled() before passing it to ImageView.
if(resource!==null && !resource.isRecycled)
imageView.setImageBitmap(resource)