java - 相机 Intent ACTION_IMAGE_CAPTURE 并放置额外数据

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

[编辑] 这是我最后如何处理这个问题,它也许可以帮助某人......

我只是在同一个类(class)中“共享”我的 ID:

private String inputid;

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

但是如果有人有更好的解决方案,我会采纳!

问候......

最佳答案

我不知道这是否是一个好的编程想法,但我创建了 Uri,我将其作为此类的私有(private)静态成员传递 Intent ,并创建了一个公共(public)静态函数来返回此 Uri,因为在我的情况下,我是在该类的 fragment 类之一中返回结果,在其( fragment 的类) onActivityResult 中,而不是使用 Intent Data 参数,该参数为 null(正如我发现的),我直接通过调用函数(我创建的)来使用该 Uri返回 Uri)。而且......它起作用了:)

关于java - 相机 Intent ACTION_IMAGE_CAPTURE 并放置额外数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16021828/

相关文章:

java - 从导航 View (抽屉)动态检索对象 android/java

ios - 错误 noCamerasAvailable,仅适用于 iPhone <8+ Swift

java - 我如何在 Spring 中使用可变属性文件名?

java - Android:SoundPool/录制音频

android - Xamarin Forms - 如何检查 Android 上 RequestIgnoreBatteryOptimizations 权限的设置

android - Camera2 API 设置自定义白平衡和色温

android - Android 上的相机方向

java - Google Cloud Spanner 与 ORM (hibernate) 集成

java - 为什么此 Java Robot 代码会关闭我的计算机?

java - 如何在 Joda Time 上计算带负号的 2 个周期的总和