活动完成时通知按钮监听器

时间:2015-04-07 19:14:49

标签: android android-intent onclicklistener

我有一个Button,我在其中附加一个监听器来捕捉照片。拍摄照片并存储到文件后,我需要做一些后期处理。我怎么能这样做?

这里是按钮启动时的代码

    cameraButton = (ImageButton) rootView.findViewById(R.id.camera_button);
    cameraButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            String fileName = mediaStorageDir.getPath() + File.separator +
                    "IMG_"+ timeStamp + ".jpg";

            Intent intent = new Intent(v.getContext(), CameraCapture.class);
            intent.putExtra("FILE_NAME", fileName);
            startActivityForResult(intent, 100);

            // TODO: do something extra. Right now it does not work
            IWantToDoSomethingExtra();
        }
    });

和相机

public class CameraCapture extends Activity {
    private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
    private String fileName;
    private File mediaFile;
    Intent intent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.clothes_frame);

        intent = getIntent();
        fileName = (String) intent.getExtras().get("FILE_NAME");
        mediaFile = new File(fileName);
        Uri fileUri = Uri.fromFile(mediaFile);

        Intent mediaIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        mediaIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        startActivityForResult(mediaIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                setResult(Activity.RESULT_OK);
                Toast.makeText(this, "New image captured", Toast.LENGTH_LONG).show();
            }
            else if (resultCode == RESULT_CANCELED) {
                Toast.makeText(this, "Photo request canceled", Toast.LENGTH_LONG).show();
            }
            else {
                Toast.makeText(this, "Bugs happened. Please contact us", Toast.LENGTH_LONG).show();
            }
        }
        finish();
    }
}

0 个答案:

没有答案