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

kon*_*kea 4 android android-camera

我希望通过在FirstActivity中单击按钮Capture捕获后显示图像,并使用SecondActivity在activity_second(布局)中显示图像.

FirstActivity

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);
            }
        });
}
Run Code Online (Sandbox Code Playgroud)

布局活动_第一

> <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>
Run Code Online (Sandbox Code Playgroud)

SecondActivity

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

        ImageView view = (ImageView) findViewById(R.id.view_photo);
    }
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

G_S*_*G_S 5

使用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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

接下来在下一个活动中尝试使用它

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.
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮到你