Camera Intent ACTION_IMAGE_CAPTURE并输入额外数据

时间:2013-04-15 18:03:58

标签: java android camera bundle extra

我想将额外的数据添加到Intent相机中,这看起来很简单......

它在几天前工作,但我对代码,avd和目标版本做了很多更改,现在它无法正常工作。

该项目现在是目标版本11.

实际上我的目标是从我的数据库传递一个id来构建图像名称,但这里有一个简单的代码来说明我的问题。

以下是示例代码:

<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;
    }
}

[编辑] 在这里我最终如何处理这个问题,它可以帮助某人......

我只是在同一个班级“分享”我的ID:

private String inputid;

当它被Camera请求调用时,我填充inputid字符串中的id,显然在OnresultActivity上,这个字符串不能为null,所以它就像那样简单...

但如果有人有更好的解决方案,我会接受它!

...问候

1 个答案:

答案 0 :(得分:0)

我不知道如果它是一个很好的编程想法,但我创建了Uri,我作为此类的私有静态成员传递意图并创建了一个公共静态函数来返回此Uri,因为在我的情况下我在这个类的一个片段类中返回结果,在它的(片段&#39;类)onActivityResult而不是使用null的Intent Data参数(我发现)我直接通过调用使用了这个Uri函数(我创建的函数返回Uri)。而且......它有效:)