【问题标题】:Camera Intent ACTION_IMAGE_CAPTURE and put extra data相机意图 ACTION_IMAGE_CAPTURE 并放置额外数据
【发布时间】:2013-04-15 18:03:57
【问题描述】:

我想将额外的数据放入 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;

当它被相机请求调用时,我在 inputid 字符串中填充了 id,显然在 OnresultActivity 上这个字符串不能为空,所以它的工作方式就这么简单......

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

问候....

【问题讨论】:

  • 您希望 Intent 返回什么 - 看起来您不能只是将任意附加内容放入您发送到 Camera ActivityIntent 中。看官方文档:developer.android.com/guide/topics/media/…
  • 感谢您的回复。我想你是对的。我找到了一种解决方法,现在我正在唱 MediaStore.Images.Media.DESCRIPTION 以将数据传递给 onactivityresult,它是专用于相机参数的输入 android 对象之一。但如果有更好的解决方案,我会感兴趣...

标签: java android camera bundle extra


【解决方案1】:

我不认为这是一个好的 prog 想法,但我创建了 Uri,我将其作为此类的私有静态成员传递,并创建了一个公共静态函数来返回此 Uri,因为在我的情况下,我是在此类的片段类之一中取回结果,在其(片段的类)onActivityResult 中,而不是使用为空的 Intent Data 参数(如我发现的那样),我通过调用函数(我创建的)直接使用了该 Uri返回 Uri)。而且....它起作用了:)

【讨论】:

  • 虽然这避免了空指针异常,但我没有得到我点击的图像:(
猜你喜欢
  • 1970-01-01
  • 2011-10-12
  • 1970-01-01
  • 1970-01-01
  • 2018-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多