Search code examples
cameraandroid-viewpagerandroid-2.3-gingerbread

Android ViewPager and Camera Preview issue on Gingerbread


I did extensive documentation study on ViewPagers and on Camera apps, infact everything works quite fine, except for a major issue on GingerBread, which I haven't found any question about on S.O. so I'll try asking this one...

In my application there is a ViewPager (support library v4) which shows two fragments: One, will call it the main fragment, the one showing when the activity starts, certain information are shown to the user, including an imageview, which is initially empty and hidden.

By swyping to the next fragment, the user sees the camera preview, which gets created when the activity is created (even if it not showing until the swype) and the button to take a picture.

When the picture is taken, the view is programmatically taken back to the main fragment, the imageview is loaded with the new photo and unhidden so the user can see the image mixed with the other preexisting information.

If the user does not like picture, he can swype back again to the camera fragment and take another one, and so all over until he is satisfied with the result.

For this reason, before flipping back to the main fragment, I call mCamera.restartPreview() and ensure the camera is ready should the user swype back.

Everything works smoothly and fine on Android 4.x, but when I test this on a 2.3.3 (API Level 10) the camera preview remains in foreground when the main fragment is called back, and hides the view. You can see, and even scroll, the remainder of the view, in the portion of teh screen where the camerafragment shows the buttons, but the rest is overlapped by the camera preview.

Here is the code of the relevant functions:

CameraFragment onPictureTaken()

private PictureCallback mPicture = new PictureCallback() {

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
        if (pictureFile == null){
            Log.d(TAG, "Error creating media file, check storage permissions: ");
            return;
        }

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            Log.e(TAG, "FILE " +pictureFile.getAbsoluteFile().toString());
            Bitmap bitmap = rotated(data);
            bitmap.compress(CompressFormat.JPEG, PICTURE_QUALITY, fos);
            bitmap.recycle(); //devo davvero chiamarla esplicitamente ??

            mediaFile = pictureFile;
            mPreview.restartPreview();
            // go back to newpin activity to show it
            ((NewPinActivity) myActivity).newPin.setMedia(mediaFile.getAbsoluteFile().toString());
            ((NewPinActivity) myActivity).takeBack();
        } catch (FileNotFoundException e) {
            Log.d(TAG, "File not found: " + e.getMessage());
        }
    }
};

and the mainfragment takeBack() method

public void takeBack(){
    mViewPager.setCurrentItem(0, true);
    String mainFragTag = "android:switcher:"+R.id.newpinpager+":0";
    Fragment fragMain = this.getSupportFragmentManager().findFragmentByTag(mainFragTag);

    try {
        ((NewPinMainFragment)fragMain).showPhoto(newPin.getMedia());
    } catch (NullPointerException e){
    }
}

Does anybody have a clue if this is an issue with the ViewPager and GingerBread or with the CameraPreview and Gingerbread or what ?


Solution

  • I found the answer by myself, I'll post it here in case it's going to be useful to others.

    Apparently the problem was cause by the smoothscroll option of setCurrentItem method. The issue was fixed by changing

    mViewPager.setCurrentItem(0, true);
    

    to

    mViewPager.setCurrentItem(0, false);