我正在嘗試使用我的Android應用拍照,但是當我拍攝圖像時,相機顯示屏不會消失。我正在使用的代碼如下。我還在Google的開發者網站SDK上使用了SDK。Android相機活動保持開啓狀態
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;
}
}
非常感謝!它正在工作!還有一個問題(我仍然給你的聲譽),但我怎麼能叫照片庫? –
沒問題;-)調用圖庫:'Intent intent = new Intent(); intent.setType(「image/*」); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(意圖。createChooser(intent, 「Select Picture」),1);' 或者參見http://stackoverflow.com/questions/2169649/get-pick-an-image-from-androids-built-in-gallery-app-以編程方式 – Darwind
再次感謝@Darwind! –