构建相机应用程序 - 接收

时间:2011-12-27 13:42:24

标签: java android

我是Android编程的新手,我正在用Java编写一个应用程序,打开相机拍照并保存。我是通过 Intents 制作的,但我看不到onActivityResult正在运行。

我已将其测试到我的手机(三星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);
}
}

1 个答案:

答案 0 :(得分:0)

尝试以下代码,你必须稍微修改它,它将帮助你从图书馆和从相机,SELECT_PICTURE用于从图书馆获取图像

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case SELECT_PICTURE:
            Uri selectedImageUri = data.getData();
            filemanagerstring = selectedImageUri.getPath();
            selectedImagePath = getPath(selectedImageUri);
            if (selectedImagePath != null)
                myFile = new File(selectedImagePath);
            else if (filemanagerstring != null)
                myFile = new File(filemanagerstring);
            if (myFile != null) {
                Bitmap bmp_fromGallery = decodeImageFile(selectedImagePath);


        break;
    case CAMERA_REQUEST:

            Bitmap bmp_Camera = (Bitmap) data.getExtras().get("data");

        break;
    default:
        break;
    }
}