我是新來的Android編程,我正在用Java編寫一個應用程序,打開相機拍照並保存。我通過意向,但我看不到onActivityResult
正在運行。建立一個相機應用程序 - 接收
我已將其測試到我的手機(Samsung Galaxy S)中,當我拍照時,我會收到該照片的預覽,其中有兩個按鈕一個保存,另一個取消。我沒有添加一些東西給我的代碼來做到這一點,所以我認爲這是相機所做的事情。我想在捕獲圖像後運行onActivityResult
(在按下預覽上的保存按鈕之後)。
但是,在預覽按下按鈕保存後,我將如何返回結果以開始onActivityResult
?
我忘了說,我按保存後,我的整個應用程序終止。 這裏是我的代碼
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TakePicButton = (Button) findViewById(R.id.TakePicture);
TakePicButton.setOnClickListener((android.view.View.OnClickListener) this);
}
@Override
public void onDestroy(){
super.onDestroy();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
} else {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.TakePicture){
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
}
'onActvityResult()'應該在按下保存按鈕後自動調用。沒有一些代碼很難說出這裏有什麼問題,所以請張貼一段代碼片斷,展示如何通過intent和完整的'onActivityResult()'方法調用相機活動。謝謝。 – 2011-12-27 13:49:23
嘿,我把我的代碼。如果你想到的東西告訴我。謝謝 – 2011-12-27 14:31:06