Attempt to invoke virtual method 'void android.graphics.Bitmap.copyPixelsFromBuffer(java.nio.Buffer)' on a null object reference
Solution 1
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());
Solution 2
"yes, btmp is null"
But, you trying to invoke method call on it:
btmp.copyPixelsFromBuffer(buffer_temp); // <- here
That not going to work. You should initialize btmp
before using it.
Update:
Init it like this:
...
System.out.println("Values are "+ bitmap.getAllocationByteCount());
// here's the initialization
Bitmap btmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
buffer_temp.rewind();
// now you can call copyPixelsFromBuffer() on btmp
btmp.copyPixelsFromBuffer(buffer_temp);
...
Alexander Zaldostanov
Updated on October 05, 2020Comments
-
Alexander Zaldostanov over 3 years
I have a
ImageView
I am getting bitmap out of that, and then usecopyPixelstoBuffer
and I am copying it tobuffer_temp
, now I want to use reverse algorithm to again convert it to another bitmap and from that bitmap toImageView2
,what exactly I am doing is Copying an Image in
ImageView
usingBuffer
andPasting
it onto anotherImageview
usingBuffer
, but while CopyingcopyPixelsFromBuffer
always throwjava.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(); }