0

I have a app that takes a picture and then I want to see that picture when its taken. And that works fine, I get a picture and save it to storage. So I made a view for it called "activity_image.xml. But I get this nullpointer:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference

Here I try to pass it to imageview from my main activity, and I cant figure out how to do this properly:

static final int REQUEST_TAKE_PHOTO = 1;
static final int REQUEST_IMAGE_CAPTURE = 1;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
private Bitmap mImageBitmap;
private String mCurrentPhotoPath;
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_fullscreen);

    mVisible = true;
    mControlsView = findViewById(R.id.fullscreen_content_controls);
    mContentView = findViewById(R.id.fullscreen_content);
    sButton = findViewById(R.id.sButton);
    //final Intent cam = new Intent("android.media.action.IMAGE_CAPTURE");

    // Set up the user interaction to manually show or hide the system UI.
    mContentView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            toggle();
        }
    });

    sButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view){
            dispatchTakePictureIntent();
        }
    });

    // Upon interacting with UI controls, delay any scheduled hide()
    // operations to prevent the jarring behavior of controls going away
    // while interacting with the UI.
    findViewById(R.id.sButton).setOnTouchListener(mDelayHideTouchListener);
}

    private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
            System.out.println("ERR CANNOT CREATE FILE");
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.example.android.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}
private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        try {
            mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));

            mImageView = (ImageView)findViewById(R.id.imageView2);
            mImageView.setImageBitmap(mImageBitmap);


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Activity_image.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:id="@+id/imageView2"
        android:layout_weight="0.95" />
</LinearLayout>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
NicklasN
  • 484
  • 1
  • 7
  • 20

4 Answers4

1

change this

mImageView.setImageBitmap(mImageBitmap);
mImageView = (ImageView)findViewById(R.id.imageView2);

to

 mImageView = (ImageView)findViewById(R.id.imageView2); mImageView.setImageBitmap(mImageBitmap);

your imageview is in activity_image .xml, but oncreate method you have used activity_fullscreen.xml , therefore you have to use layout inflater here and view. for eg:view v; Imageview image=(Imageview)v.findViewById(R.id.imageView2);

vimal raj
  • 295
  • 1
  • 13
1
mImageView = (ImageView)findViewById(R.id.imageView2);
mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));
                mImageView.setImageBitmap(mImageBitmap);

Just Reformate your code like this
change this :

setContentView(R.layout.activity_image);
Milind Vyas
  • 294
  • 5
  • 12
0

Replace

 mImageView.setImageBitmap(mImageBitmap);
 mImageView = (ImageView)findViewById(R.id.imageView2);

with

mImageView = (ImageView)findViewById(R.id.imageView2);
mImageView.setImageBitmap(mImageBitmap);

EDIT:

setContentView(R.layout.activity_fullscreen);

So, your imageView2 ImageView should be in activity_fullscreen. But what is Activity_image.xml?

Seshu Vinay
  • 13,560
  • 9
  • 60
  • 109
0

Your problem is here:

mImageView = (ImageView)findViewById(R.id.imageView2); // first set the var
mImageView.setImageBitmap(mImageBitmap); // then set the image view

aswell check this:

setContentView(R.layout.activity_fullscreen); // this should link to the right XML file. At the moment it looks like it doesn't.