Junaid
Junaid

Reputation: 7860

Handling orientation change on Camera Intent

I use the inbuilt camera application from my Activity to take a picture. The problem is that I am not able to handle the data returned from camera Intent if the orientation changes and my activity is redrawn, I tried to search for the same problem but it seems to confuse me more. Do I go for the bundle thing or I go for OnConfigurationChanged() I have placed the android:configChanges="orientation" in activity tags in Manifest file,for this particular activity which calls the camera intent, but the problem seems to persist. Can anyone please guide me to a solution.

Here is my Code:

if(i==0){
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); 
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if( requestCode == 1337 && resultCode== Activity.RESULT_OK){
        Bundle extras = data.getExtras();

        if (extras != null){
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 1;
            options.inPurgeable = true;
            options.inInputShareable = true;
            thumbnail = (Bitmap) data.getExtras().get("data");
            // imgview.setImageBitmap(thumbnail);
            image(thumbnail);

        }else{
            Toast.makeText(CreateProfile.this, "Picture NOt taken", Toast.LENGTH_LONG).show();
        }
        super.onActivityResult(requestCode, resultCode, data);     
    }
}

Upvotes: 1

Views: 1687

Answers (3)

Yalla T.
Yalla T.

Reputation: 3727

First of All, you have a problem in your code.

You ask if if (extras != null){ but still get you bitmap from data thumbnail = (Bitmap) data.getExtras().get("data"); This Line should be

thumbnail = (Bitmap) extras.get("data");

Second, dont use android:configChanges, it messes up a lot of other things, and most apps shouldnt use it.

If you mean a orientation change after you have gotten the data, using saveInstanceState works good. try something like this, just put your data that you need inside the outState Bundle and get it back in the onRestoreInstance method

@Override
protected void onSaveInstanceState(Bundle outState) {
 outState.putAll(extras);
 super.onSaveInstanceState(outState);

}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {

 super.onRestoreInstanceState(savedInstanceState);
 extras.putAll(savedInstanceState);
}

Upvotes: 2

anzaidemirzoi
anzaidemirzoi

Reputation: 406

if the target sdk of your app is bigger than 12 change it to 12 and try again pelase.

android:targetSdkVersion="12"

and also do not forget to add android:configChanges="orientation|keyboardHidden" this to your activity line in the manifest

Upvotes: 1

Pratik Sharma
Pratik Sharma

Reputation: 13405

Try to add this attribute to your activity in the Manifest file.

android:configChanges="keyboardHidden|orientation|screenSize"

Thanks.

Upvotes: 2

Related Questions