Bahu
Bahu

Reputation: 1596

Attempt to invoke virtual method 'int android.graphics.Bitmap.getHeight()' on a null object reference

I'm working on image selection from "Gallery" or "Take photo". My problem is, while back pressed when taking the photo by using camera. I'm getting the error.

My code is

if (items[position].equals("Take photo")) {
                        Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        File file= getOutputMediaFile();
                        Uri picUri = Uri.fromFile(file);
                        filepath = picUri.getPath();
                        i.putExtra(MediaStore.EXTRA_OUTPUT,picUri);
                        _a.startActivityForResult(i, 1);
                    }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    Log.e("Action", action);
    if (action.equals("add_car")) {
        if(_addLayout.filepath != null){
            filePath = _addLayout.filepath;
            _addLayout.filepath = null;
        }
        if(data != null)
        filePath = CommonUtilities.getPath(data.getData(), "Image");
        _addLayout.filepath = filePath;
        setImage(UI_AddCar.ivTakenPicture);
    }
  }

void setImage(ImageView im){
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 4;

     Bitmap bitmap = BitmapFactory.decodeFile(filePath);

     if(filePath != null){
         int height = bitmap.getHeight(), width = bitmap.getWidth();
            if (height > 1280 && width > 960){
                Bitmap imgbitmap = BitmapFactory.decodeFile(filePath, options);
                im.setImageBitmap(imgbitmap);
                im.setVisibility(View.VISIBLE);
         } else {
                im.setImageBitmap(bitmap);
                im.setVisibility(View.VISIBLE);
            }
     }

In this UI_AddCar.ivTakenPicture is the ImageView

Upvotes: 0

Views: 12084

Answers (1)

Bahu
Bahu

Reputation: 1596

Thanks to Piyush Gupta. I updated if condition like

if(filePath != null && bitmap != null){
         int height = bitmap.getHeight(), width = bitmap.getWidth();

            if (height > 1280 && width > 960){

                    Bitmap imgbitmap = BitmapFactory.decodeFile(filePath, options);
                    im.setImageBitmap(imgbitmap);
                    im.setVisibility(View.VISIBLE);

            }else {

                    im.setImageBitmap(bitmap);
                    im.setVisibility(View.VISIBLE);
            }
     }

Upvotes: 2

Related Questions