Android相机活动保持打开状态

时间:2013-04-02 19:17:34

标签: android android-camera

我正在尝试用我的Android应用拍照,但是当我拍摄图像时,相机显示屏不会消失。我正在使用的代码如下。我还在Google的开发者网站SDK上使用了SDK。

 import android.os.Bundle;
 import android.app.Activity;
 import android.content.Intent;
 import android.graphics.Bitmap;
 import android.graphics.Camera;
 import android.view.Menu;
 import android.view.MotionEvent;
 import android.view.View;
 import android.widget.Button;
 import android.widget.ImageView;

 public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button mainButton = (Button)findViewById(R.id.mainBtn);
    mainButton.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            //Intent userCreationIntent = new Intent(v.getContext(), SecondviewActivity.class);
            //startActivity(userCreationIntent);
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            startActivityForResult(cameraIntent, CAMERA_REQUEST); 

            return false;
        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        ImageView imageView = (ImageView)findViewById(R.id.imageView1);
        imageView.setImageBitmap(photo);
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    }  
} 

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}

2 个答案:

答案 0 :(得分:1)

这种情况正在发生,因为您没有附加路径,意图告诉Android在哪里存储图像。我也有同样的问题。

尝试以下代码:(我刚刚添加了4行)

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Camera;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
    private static final int CAMERA_REQUEST = 1888; 
    // Creating the Uri where Camera saves a picture each time
    String imagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + System.currentTimeMillis() + "_myCameraImage.jpg";
    File imageFile = new File(imagePath);
    imageUri = Uri.fromFile(imageFile);

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   Button mainButton = (Button)findViewById(R.id.mainBtn);
   mainButton.setOnTouchListener(new View.OnTouchListener() {

   @Override
   public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        //Intent userCreationIntent = new Intent(v.getContext(), SecondviewActivity.class);
        //startActivity(userCreationIntent);

        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
        cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);

        startActivityForResult(cameraIntent, CAMERA_REQUEST); 

        return false;
        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        ImageView imageView = (ImageView)findViewById(R.id.imageView1);
        imageView.setImageBitmap(photo);
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    }  
} 

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
   }
}

在复制并粘贴此代码之前,请确保您的代码中有必要的import语句,例如File,Uri等的import语句。

我强烈希望它现在能够奏效:)

答案 1 :(得分:1)

问题出在onTouchListener的{​​{1}}范围内!

我刚刚运行了您的代码并将Button更改为onTouchListener代替了onClickListener,代码正常运行。

请在此处查看我修改后的代码:

Button

我不确定,为什么你使用import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { private static final int CAMERA_REQUEST = 1888; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button mainButton = (Button) findViewById(R.id.mainBtn); mainButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent cameraIntent = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_REQUEST); } }); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAMERA_REQUEST) { Bitmap photo = (Bitmap) data.getExtras().get("data"); ImageView imageView = (ImageView) findViewById(R.id.imageView1); imageView.setImageBitmap(photo); } } } 作为onTouchListener,但它显然不起作用 - 实际上是非常奇怪的行为; - )

编辑:只是一个小小的更新。我尝试调试代码,如果您使用Button代替onTouchListener,则在点击按钮时实际触发3 onClickListenerMotionEvents,{{1 }和MotionEvent.ACTION_DOWN

现在,第一个事件MotionEvent.ACTION_MOVE将触发您展示相机的意图,当您拍完照片后单击确定时,下一个MotionEvent.ACTION_UP MotionEvent.ACTION_DOWN正在排队等候被触发,这将再次发送到相机活动。现在再拍摄一张照片并点击“确定”后,您将返回到您的活动,现在最后一个MotionEvent MotionEvent.ACTION_MOVE正在排队,并触发第三次调用相机活动。在最后一次相机通话后,您将能够毫无问题地恢复活动; - )

为什么MotionEvent没有这样做,是因为它处理的是“完整”点击,可能是MotionEvent.ACTION_UPonClickListenerMotionEvent.ACTION_DOWN所有3个事件正在发生一次点击。

希望这会有所帮助; - )