3

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();
            }       
2
  • did you initialize btmp? Commented Aug 1, 2015 at 19:35
  • use a debugger and check the contents of your buffer_temp after buffer_temp.rewind() this error is only possible when either buffer_temp is null or btmp is Commented Aug 1, 2015 at 19:40

2 Answers 2

2

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());
Sign up to request clarification or add additional context in comments.

6 Comments

yes, btmp is null, because I want a new bitmap into ImageView2
Paste way more code. You have serious issue back there.
so what should I do, because I want to use new Bitmap object
its the same exception which I have described, you can go ahead and do that, take two ImageView and one button, and try the above code snippet, it keeps crashing
If the code above throws then you dont have any Bitmap at the first place
|
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);
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.