相机不会在Android代码中启动

时间:2015-10-18 19:36:26

标签: android eclipse

我写这段代码ti拍照然后发送到其他活动。但是每次我运行它时,相机剂量都会开始,而且只有在拍照类中才会开始新的活动。如果我删除这部分相机开始:

  Intent intent2 = new Intent (welcom.this, MainActivity.class);
    startActivity(intent2);
    intent2.putExtra("mImageUri", imageUri);

代码:

public class welcom extends Activity{
private static final int TAKE_PICTURE = 1;    
private Uri imageUri;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcom);

    Button camera= (Button) findViewById(R.id.camera);
    Button gallery= (Button) findViewById(R.id.gallery);


    camera.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {




            takePhoto();




        }
    });

gallery.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

        }
    });
}








public void takePhoto() {
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo = new File(Environment.getExternalStorageDirectory(),  "Pic.jpg");
    intent.putExtra(MediaStore.EXTRA_OUTPUT,
            Uri.fromFile(photo));
    imageUri = Uri.fromFile(photo);
    startActivityForResult(intent, TAKE_PICTURE);

    Intent intent2 = new Intent (welcom.this, MainActivity.class);
    startActivity(intent2);
    intent2.putExtra("mImageUri", imageUri);
}}

2 个答案:

答案 0 :(得分:0)

试试这个:

Intent intent2 = new Intent (welcom.this, MainActivity.class);
intent2.putExtra("mImageUri", imageUri);
startActivity(intent2);

答案 1 :(得分:0)

您首先打开新活动,然后将图像添加到其中。洗牌顺序和问题解决了。

其他:

您必须通过将照片转换为字节数组来传递照片。看看它解决问题的那个问题

Take photo and pass it to another activity