Try invoking the virtual method 'void android.graphics.Bitmap.copyPixelsFromBuffer (java.nio.Buffer)' on a null object reference

advertisements

I have a ImageView I am getting bitmap out of that, and then use copyPixelstoBuffer and I am copying it to buffer_temp, now I want to use reverse algorithm to again convert it to another bitmap and from that bitmap to ImageView2,

what exactly I am doing is Copying an Image in ImageView using Buffer and Pasting it onto another Imageview using Buffer, but while Copying copyPixelsFromBuffer always throw

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.Bitmap.copyPixelsFromBuffer(java.nio.Buffer)' on a null object reference.

Dont know why, Need help,

try {
                Buffer bfr = null;
                iv1.setImageResource(R.drawable.olx);
                BitmapDrawable drawable = (BitmapDrawable) iv1.getDrawable();
                Bitmap bitmap = drawable.getBitmap();
                int bytes=bitmap.getByteCount();
                ByteBuffer buffer_temp= ByteBuffer.allocate(bytes);
                bitmap.copyPixelsToBuffer(buffer_temp);
                System.out.println("Values are "+ bitmap.getAllocationByteCount());

                Bitmap btmp=null;

                //btmp.copyPixelsFromBuffer(buffer_temp);

                if(buffer_temp==null)
                 return;

                buffer_temp.rewind();

                btmp.copyPixelsFromBuffer(buffer_temp);

                if(buffer_temp==null)
                {
                    Toast.makeText(getApplicationContext(), "Null", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "Not Null", Toast.LENGTH_SHORT).show();
                }

            } catch (NotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


btmp is null. There is no way that by using the attached code.Then, the value of btmp would be anything. But it's null!

If you want to clone Bitmap the use create method or any other of that sort.

Bitmap btmp = Bitmap.create(drawable.getBitmap());