myq_26 2013-05-07 03:16 采纳率: 16.7%
浏览 2103

捕捉图像,然后使用 activity 在另一个布局中显示

我想通过在 FirstActivity 中点击按钮Capture捕捉图像,然后使用SecondActivity在activity_second(layout)中显示出来。如何实现?
代码部分:
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);
            }
        });
}

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

SecondActivity

> 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>
  • 写回答

1条回答

  • hxn_217 2013-05-07 07:12
    关注

    使用 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("data");
    //set the image here.
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型