1

I want to put extra data to an Intent Camera, that seems simple...

It WAS working some days ago, but I made a lot of changes on code, avd and target version and now it is not working.

The project is now on target version 11.

In fact my goal is to pass an id from my database to construct image name but here a simple code to illustrate my issue.

Here's the sample code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".CameraTestActivity" >

    <Button
        android:id="@+id/buttonpic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="76dp"
        android:text="Button" />

</RelativeLayout>

-

public class CameraTestActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera_test);

        Button buttonpic = (Button)findViewById(R.id.buttonpic);
        buttonpic.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                ///first try to put extra data
                cameraIntent.putExtra("thisone", "ArgumentFrom");

                ///second try to put extra data
                Bundle extras = new Bundle();
                extras.putBoolean("thisalso",true);
                cameraIntent.putExtras(extras);

                ///start camera activty with ID
                startActivityForResult(cameraIntent, 1888); 
            }
        });
    }

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

        ///if pic is picked and Intent ID is Camera one i.e. 1888
        if (requestCode == 1888 && resultCode == RESULT_OK) {

            ///first try >> Not working
            Bundle extra = getIntent().getExtras();////
            if (extra != null) {
                Log.d("extra: ","isnotnull");
                Boolean inputThatIwant = extra.containsKey("thisone");
                Boolean inputThatIwantBool = extra.containsKey("thisalso");
                Log.d("thisone",inputThatIwant.toString());
                Log.d("thisalso",inputThatIwantBool.toString());
            }

            ///Second try >>  Some Data is back (pic data... ?)
            Bundle extras = data.getExtras();
            if (extras != null) {
                Log.d("extras: ","isnotnull"); /////-->>Print "isnotnull"
                Boolean inputThatIwant = extras.containsKey("thisone");
                Boolean inputThatIwantBool = extras.containsKey("thisalso");
                Log.d("thisone",inputThatIwant.toString()); /////-->>Print "false"
                Log.d("thisalso",inputThatIwantBool.toString()); /////-->>Print "false"
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.camera_test, menu);
        return true;
    }
}

[EDIT] Here how I deal with this finally, it can help someone perhaps...

I just "share" within the same class my ID:

private String inputid;

When it's called by Camera request I fill the id in the inputid String and obviously on OnresultActivity this String could not be null, so it works as simple as that...

But if someone has a better solution I'll take it!

Regards....

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
neuronsoverflow
  • 133
  • 4
  • 13
  • What do you expect the `Intent` to return - it doesn't look like you can just put arbitrary extras into the `Intent` that you sent to the `Camera Activity`. Look at the official documentation: http://developer.android.com/guide/topics/media/camera.html#intent-image – Darwind Apr 15 '13 at 19:49
  • Thank you for your reply. I think you are right. I have found a workaround and now I'm sing MediaStore.Images.Media.DESCRIPTION to pass data to onactivityresult which is one of input android object which is dedicated to camera parameters. But if there is a better solution I'll be interested... – neuronsoverflow Apr 15 '13 at 20:44

1 Answers1

0

I don't if its a good prog idea but i created the Uri which i was passing in the intent as a private static member for this class and created a public static function to return this Uri and since in my case I was getting back the result in one of the fragment class of this class , in its(fragment's class) onActivityResult instead of using the the Intent Data parameter which was null(as i found) i used that Uri directly by calling the function(that i created to return Uri). And.... it worked :)

  • Although this is avoiding the null pointer exception but i am not getting the image back what i clicked :( – SamHokage Apr 23 '14 at 14:54