使用意图拍照,而不是返回应用程序?

时间:2014-12-08 00:11:25

标签: android android-intent android-camera

在我的应用程序中,我使用的目的是启动手机的相机,拍照并将其保存在手机本地。这一切都运行正常,但是,一旦我按下保存(保存图片),应用程序退出,它将带我回到手机的主屏幕。我不确定我错过了什么,所以在我按下保存后,它会回到我的应用而不是主屏幕?以下是我的代码:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //other code, removed as its unrelated      

    btnAddExpense.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //Toast.makeText(context, "List Item Clicked.", Toast.LENGTH_LONG).show();

            // create intent with ACTION_IMAGE_CAPTURE action 
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            // this part to save captured image on provided path
            File file = new File(Environment.getExternalStorageDirectory(), "AutoTracker_" + System.currentTimeMillis() + ".jpg");
            Uri photoPath = Uri.fromFile(file);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, photoPath);

            // start camera activity
            startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
        }
    });    
        return rowView;          
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
      if (resultCode == Activity.RESULT_OK) {
          // Image captured and saved to fileUri specified in the Intent
          Toast.makeText(context, "Image saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
      } else if (resultCode == Activity.RESULT_CANCELED) {
          // User cancelled the image capture
      } else {
          // Image capture failed, advise user
      }
    }

    if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
       if (resultCode == Activity.RESULT_OK) {
           // Video captured and saved to fileUri specified in the Intent
           Toast.makeText(context, "Video saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
       } else if (resultCode == Activity.RESULT_CANCELED) {
           // User cancelled the video capture
       } else {
           // Video capture failed, advise user
       }
    }
}

谢谢!

0 个答案:

没有答案