简体   繁体   English

Android默认摄像头意图打开摄像头

[英]Android Default Video Camera Intent Opens Image Camera

I am trying to open the android default video camera from my app, using the following code: 我正在尝试使用以下代码从我的应用程序打开android默认摄像机:

Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent, 1);

But on my two phones (Samsung Note 2, and Google Pixel), it opens the image camera instead. 但是在我的两部手机(三星Note 2和Google Pixel)上,它却打开了摄像头。 I have this permission in my manifest: 我的清单中有此许可:

<uses-permission android:name="android.permission.CAMERA"/>

Any ideas what causes this issue? 任何想法导致此问题的原因是什么? I've also requested the permission at runtime. 我还要求在运行时授予许可。

You must add next code. 您必须添加下一个代码。 Devices have Android 6.0 or later. 设备具有Android 6.0或更高版本。

if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        startActivityForResult(intent, 1);
    } else {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 2);
    }

If your the app has the permission for use the STORAGE we open the STORAGE. 如果您的应用拥有使用存储的权限,我们将打开存储。

If your the app doesn't have permission for use STORAGE, we open system-dialog. 如果您的应用没有使用存储的许可,我们将打开系统对话框。 Result from the dialog you can see in the onRequestPermissionsResult. 您可以在onRequestPermissionsResult中看到的对话框的结果。 (You must override it on your Activity). (您必须在“活动”中覆盖它)。

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
        @NonNull int[] grantResults) {
    if (requestCode == 2) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
            startActivityForResult(intent, 1);
        }
    }
}

I think you must extract next lines to the private method. 我认为您必须提取下一行到private方法。

private void takePhoto() {
    Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    startActivityForResult(intent, 1);
}

For more information see : https://developer.android.com/training/permissions/requesting.html 有关更多信息,请参见: https : //developer.android.com/training/permissions/requesting.html

添加关注权限CAPTURE_SECURE_VIDEO_OUTPUT和CAPTURE_VIDEO_OUTPUT

Android 6.0 and later requieres to ask permissions at run time. Android 6.0及更高版本需要在运行时询问权限。 Read the official doc here: https://developer.android.com/training/permissions/requesting.html 在此处阅读官方文档: https//developer.android.com/training/permissions/requesting.html

I hope it helps. 希望对您有所帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM