如何将Camera Intent设置为主要活动

时间:2015-11-07 23:00:41

标签: java android mobile

我想在用户打开我的应用程序后启动相机。

现在我有这个,它工作正常。当用户启动我的应用程序时,它会自动打开相机。然而,然后用户点击"返回"拍摄图像后,按钮会打开一个空白活动。

如何让它回到相机?

public class MainActivity extends AppCompatActivity {

private static final int REQUEST_TAKE_PHOTO = 0;

// The URI of photo taken with camera
private Uri mUriPhotoTaken;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    takePhoto();
}

// Deal with the result of selection of the photos and faces.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        Uri imageUri;
        if (data == null || data.getData() == null) {
            imageUri = mUriPhotoTaken;
        } else {
            imageUri = data.getData();
        }
        Intent intent = new Intent(MainActivity.this, Result.class);
        intent.setData(imageUri);
        startActivity(intent);
    }
}

// Launch the camera to allow the user to take a photo
public void takePhoto(){
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if(intent.resolveActivity(getPackageManager()) != null) {
        // Save the photo taken to a temporary file.
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        try {
            File file = File.createTempFile("IMG_", ".jpg", storageDir);
            mUriPhotoTaken = Uri.fromFile(file);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, mUriPhotoTaken);
            startActivityForResult(intent, REQUEST_TAKE_PHOTO);
        } catch (IOException e) {
            Log.d("ERROR", e.getMessage());
        }
    }
}
}

2 个答案:

答案 0 :(得分:0)

尝试在takePhoto()而不是onStart()中调用onCreate()方法,然后将finish()方法调用onStop()

答案 1 :(得分:0)

试试吧。

        @Override
        public void onBackPressed() {
            super.onBackPressed();
            Intent intent = new Intent(this, ActivityYouWantToOpen.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            finish();
        }