android - 使用android中的另一个 Activity 从捕获中获取图像并在另一个布局中显示图像

标签 android android-camera

我想在 FirstActivity 中单击捕获按钮后显示图像,并使用 SecondActivity 在 activity_second(布局)中显示图像。

第一个 Activity

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);

    Button take_photo = (Button) findViewById(R.id.btn_capture);
    take_photo.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivity(i);
            }
        });
}

布局activity_first

> <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" >

    <Button
        android:id="@+id/btn_capture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="21dp"
        android:text="Capture" />

</RelativeLayout>

第二个 Activity

> public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        ImageView view = (ImageView) findViewById(R.id.view_photo);
    }

activity_second

> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/view_photo"
        android:layout_width="260dp"
        android:layout_height="374dp" />

</LinearLayout>

最佳答案

使用 startActivityForResult() 而不是 startActivity()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    Bitmap thumbnail = null;
    if (requestCode == CAMERA_PIC_REQUEST) {
        if (resultCode == RESULT_OK) {
            thumbnail = (Bitmap) data.getExtras().get("data");
            Intent i = new Intent(this, NextActivity.class);
            i.putExtra("name", thumbnail);
            startActivity(i);
        }
    }
}

接下来在下一个 Activity 中尝试使用这个

protected void onCreate(Bundle savedInstanceState) {
    //TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    //intialize the image view 

    Bitmap bitmap  = getIntent().getExtras().getParcelable("name");
    //set the image here.
}

希望对你有帮助

关于android - 使用android中的另一个 Activity 从捕获中获取图像并在另一个布局中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11927710/

相关文章:

java - 我需要通过邮件从相机发送图像

android - 是否可以仅在带有 cordova 的手机上以纵向模式锁定 android 应用程序?

android - 如何在电话到达android时不完成 Activity ?

android - 如何在 Android 中仅更新现有 Json 对象中对象的非空 Json 字段

android - 如何计算android中自定义相机中的剩余照片数

android - Android Eclipse 模拟器上的相机 :

Android 11 后台 FCM,仅数据且屏幕关闭

android - Android 中代码的可重用性

java - Android AutoFocusCallback 未被调用或未返回

android - 从android中的图库中裁剪图像