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.

0 Answers