简体   繁体   English

如何将相机意图设置为主要活动

[英]How do I set the Camera Intent as the Main Activity

I want to launch the camera once a user opens up my application. 用户打开我的应用程序后,我想启动相机。

Right now I have this, and it works fine. 现在我有了这个,它工作正常。 When the user launches my application, it automatically opens up the camera. 当用户启动我的应用程序时,它会自动打开相机。 However, then the user hits the "back" button after taking an image, it opens up a blank activity. 但是,然后用户在拍摄图像后单击“后退”按钮,这将打开空白活动。

How do I get it to go back to the camera? 如何获得返回相机的信息?

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());
        }
    }
}
}

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

Try it. 试试吧。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将意图数据设置为当前活动editText? - How do I set intent data to current activity editText? 如果设置了变量,为什么在主活动中没有值? - Why I do not have the values in main activity if I set the variable? 如何获得使用意图打开的活动的参考? - How do I get the reference of an activity I opened using intent? 我如何从BroadcastReceiver活动到主要活动获得价值 - how do I get value from BroadcastReceiver activity to main activity 如何使用arrayAdapter中的intent进入第三个活动? - How do I use intent from arrayAdapter to go into a third activity? 如何使用意图将ArrayList传递给另一个Activity? - How do I pass ArrayList to another Activity using intent? 如何将意图从活动传递给扩展LinearLayout的类 - How do I pass an intent from an activity to a class extending LinearLayout 如何直接打开非主要活动的Android应用程序,然后再转到主要活动? - How do I open an Android app straight to an activity that is not the main activity and later move to the main activity? 如何使用意图在主要活动布局顶部打开活动? - How to open an activity on top of the main activity layout using intent? 当我旋转相机应用程序中的屏幕并且我的主要活动失败时,Android Camera无法将照片返回主要活动 - Android Camera not returning photo to main activity when I rotate the screen in the camera application and my main activity fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM