0

I'm trying to select a picture from gallery , get bitmap of that picture and encode that bitmap into base64. But I get this error:

Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference

Here is what I've done :

 private Bitmap bitmap;
 private String encodedbitmap;

 ...
 Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType("image/*");
    startActivityForResult(intent, 100);
 ...

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(requestCode==100 && resultCode==RESULT_OK ){

        Uri selectedimg=data.getData();

        new Encodeimg().execute();

    }

 ...

 private class Encodeimg extends AsyncTask<Void,Void,Void>{
    @Override
    protected Void doInBackground(Void... params) {

        bitmap=BitmapFactory.decodeFile(selectedimg.getPath());

        ByteArrayOutputStream stream= new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);
        byte[] bytes=stream.toByteArray();
        encodedbitmap= Base64.encodeToString(bytes,0);
        return null;
    }

EDIT: I know what a NullPointerException is. I just don't understand why I get that error in this situation. Because I don't see a null object here.

  • What is the question? Dont call compress on a null pointer i would say. Did you ever google fkr this ecception? – greenapps Jul 24 '16 at 20:26
  • My intention and code is clear enough , I wrote it. Looking for an answer why I'm getting that error –  Jul 24 '16 at 20:29
  • You're getting the error because `bitmap` is null, obviously, because `BitmapFactory.decodeFile` returned null. – Darko Kenda Jul 24 '16 at 23:01

0 Answers0