如何处理" back"相机打开时按钮

时间:2015-04-15 09:21:56

标签: android android-intent sdk android-camera android-camera-intent

如何按下“后退”按钮和相机按钮?就像,我有一个开放的相机拍照,但没有拍摄照片我按下硬件“后退”按钮。发生这种情况时,我的应用程序崩溃了。

如何处理按下“后退”按钮?

if (requestCode == 100) {
        if (resultCode == RESULT_OK) {
            if (intent == null) {
                // The picture was taken but not returned
                /*Toast.makeText(
                        getApplicationContext(),
                        "The picture was taken and is located here: "
                                + fileUri.toString(), Toast.LENGTH_LONG)
                        .show();  */              
            } else {
                // The picture was returned
                Bundle extras = intent.getExtras();
                temp_photo = (Bitmap) extras.get("data");
                image1.setImageBitmap(temp_photo);

            }
        }
        else if(resultCode == RESULT_CANCELED)
        {
                if (intent.resolveActivity(getPackageManager()) != null) {
                    startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
                }
                else
                {                 
                    finish();
                }
        }
    }        

2 个答案:

答案 0 :(得分:1)

您可以使用以下方法告诉应用程序按下后退按钮时要执行的操作:

@Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
       if ((keyCode == KeyEvent.KEYCODE_BACK)) {
           onBackPressed();
       }
       return true;
    }
   @Override
    public void onBackPressed() {
        super.onBackPressed();
// Enter your code here

}

希望这会对你有所帮助。

答案 1 :(得分:1)

你不必处理背压,这是由相机应用程序完成的。 您的应用程序崩溃,因为在RESULT_CANCELED为空的情况下意图。 如果你想重新启动"相机你应该再做一个新的意图:

Intent cameraIntent = new  Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, CAMERA_REQUEST);