0

I am trying to take photo from camera intent and add some textview on image (overlay) and then saving this mixed image. For this I am using below code.

MAIN PROBLEM: This code working perfectly when I put it under button's on click listner. but when I put it in "onCreate" method, its showing an error "Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference".

I searched a lot for this. Tried almost every solution provided in stackvoerflow.

   private void saveImage() {
        try {
            frameLayout.setDrawingCacheEnabled(true);
            frameLayout.buildDrawingCache();
//            drawingCache = BitmapFactory.decodeResource(getResources(), R.id.imageview);
//            drawingCache = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
            drawingCache = frameLayout.getDrawingCache();
            String timeStamp = new SimpleDateFormat("ddMMyyyy_hhmmss").format(new Date());
            saveImageFile = new File(path, "my_folder" + timeStamp + ".jpg");
            OutputStream fOut = new FileOutputStream(saveImageFile);
            tempPhoto = saveImageFile;
            drawingCache.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
            fOut.flush();
            fOut.close();
            Toast.makeText(this, "Image Saved Successfully", Toast.LENGTH_SHORT).show();
            deleteTempImage(mCurrentPhotoPath);
//            Toast.makeText(this, mCurrentPhotoPath, Toast.LENGTH_LONG).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

I want to perform saveImage function at onCreate method, otherwise user will force to click "save" button each time. Please help me. I spent almost 2 days for this.

Pooja
  • 475
  • 5
  • 14
  • *"I am trying to take a photo from camera intent and add some textview on the image (overlay) and then saving this mixed image"*. Then you say that you want to save an image in the onCreate method. It seems that you are not aware of the life cycle. If you are getting an image from a camera, you would receive it in onActivityResult, from there you can store the value and then save at certain lifecycle state you choose. I would suggest you rethink what you are trying because there could be better ways as well. About the error, it is because of a null value. – Taseer May 03 '19 at 18:48
  • Its my mistake that I have not put whole code. Actually I am taking photo from another activity and getting image data at onCreate method already. I am calling "saveImage" function after getting image data. I already mentioned that everything is working fine if I put this code under button's onclick listener. Thanks for reply. – Pooja May 03 '19 at 19:04
  • Your `Activity`'s `View`s will not yet have been measured and laid out in `onCreate()`, which is why you get null there. You need to wait until that layout to get your bitmap. You can do that a couple of ways. First, by putting your call to `saveImage()` in a `Runnable`, and `post()` it on your `frameLayout`, like is shown in [this answer](https://stackoverflow.com/a/33000863). Similarly, by putting it in an `OnGlobalLayoutListener`, like is shown in the answers on [this post](https://stackoverflow.com/q/43006389). – Mike M. May 04 '19 at 01:39
  • I would also recommend that you `draw()` your `View` to a new `Bitmap` that you create yourself, like is shown in [that first link](https://stackoverflow.com/a/33000863), rather than using the drawing cache, which can be flaky sometimes. – Mike M. May 04 '19 at 01:39
  • 1
    @MikeM. Thaks for answer. I have solved this problem by using Runnable. I will also consider your second comment for creating new `Bitmap`. Thanks again. – Pooja May 04 '19 at 05:03

0 Answers0