Android image resize after camera intent callback

TeraTon picture TeraTon · Jun 21, 2012 · Viewed 9.4k times · Source

my first question here! I have a problem with a piece of code that starts a camera intent with the extra output options and then on activity result tries to resize that image. what is happening is that a nullpointer exception gets thrown in the resize function at the callback.

The original large picture is still saved on the file system, as I can access that from the filesystem.

The original camera jpeg is 2560x1920 in size and the phone in use is a google nexus one.

I have no clear idea asto why the resize is not working, anyone have any insight?

Here's some code:

The takePicture function that also creates the dummy file:

        public boolean takePicture( ) {

            Log.e(TAG, "takePicture interface function");
            String FileUri = Environment.getExternalStorageDirectory()  + "/samples/";
            File file = new File(FileUri,"picture"+ pictureNumber +".jpg");
            try {
                file.createNewFile();
                } catch (IOException e) {
                e.printStackTrace();
                }
                Uri outputFileUri = Uri.fromFile(file);

            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
            Log.e(TAG, "intent started");
            return true;
        }

The on activity result callback function:

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

        // handle the camera request returns and handle back button in camera.

        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_CANCELED ) {
            Toast toast = Toast.makeText(this,"Canceled, no picture taken.", 1000);
            toast.show();
            return;
           }

        else if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK ) { 

            Log.e(TAG, "Camera intent return");

            Bitmap scaledphoto = null;
            int height = 300;
            int width = 300;    

            Bitmap photo = BitmapFactory.decodeFile( APP_DATA_PATH + "/samples/picture" + pictureNumber + ".jpg" );
            Log.e(TAG, "Picture fetched");

            scaledphoto = Bitmap.createScaledBitmap(photo, height, width, true);


            Log.e(TAG, "Picture scaled");

            saveImageToFile(scaledphoto, "picture" + pictureNumber + ".jpg");


            Log.e(TAG, "Scaled picture saved");

            myWebView.loadUrl("javascript:pictureTaken(\""+ pictureLoc + "\")");

            pictureNumber++;

And here's the LogCat:

06-21 14:59:13.496: E/AndroidRuntime(6130): FATAL EXCEPTION: main
06-21 14:59:13.496: E/AndroidRuntime(6130): java.lang.RuntimeException: Failure     delivering result ResultInfo{who=null, request=1888, result=-1, data=null} to activity {test.test/test.test.CameraIntentTestActivity}: java.lang.NullPointerException
06-21 14:59:13.496: E/AndroidRuntime(6130):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
06-21 14:59:13.496: E/AndroidRuntime(6130):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3023)
06-21 14:59:13.496: E/AndroidRuntime(6130):     at android.app.ActivityThread.access$1100(ActivityThread.java:123)
06-21 14:59:13.496: E/AndroidRuntime(6130):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177)
06-21 14:59:13.496: E/AndroidRuntime(6130):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-21 14:59:13.496: E/AndroidRuntime(6130):     at android.os.Looper.loop(Looper.java:137)
06-21 14:59:13.496: E/AndroidRuntime(6130):     at android.app.ActivityThread.main(ActivityThread.java:4424)
06-21 14:59:13.496: E/AndroidRuntime(6130):     at java.lang.reflect.Method.invokeNative(Native Method)
06-21 14:59:13.496: E/AndroidRuntime(6130):     at java.lang.reflect.Method.invoke(Method.java:511)
06-21 14:59:13.496: E/AndroidRuntime(6130):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-21 14:59:13.496: E/AndroidRuntime(6130):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-21 14:59:13.496: E/AndroidRuntime(6130):     at dalvik.system.NativeStart.main(Native Method)
06-21 14:59:13.496: E/AndroidRuntime(6130): Caused by: java.lang.NullPointerException
06-21 14:59:13.496: E/AndroidRuntime(6130):     at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:432)

Answer

Vipul picture Vipul · Jun 21, 2012

Change

Bitmap photo = BitmapFactory.decodeFile( APP_DATA_PATH + "/samples/picture" + pictureNumber + ".jpg" );

To

  Bitmap photo = (Bitmap) data.getExtras().get("data");