简体   繁体   中英

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:

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. 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.

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. (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 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

添加关注权限CAPTURE_SECURE_VIDEO_OUTPUT和CAPTURE_VIDEO_OUTPUT

Android 6.0 and later requieres to ask permissions at run time. Read the official doc here: https://developer.android.com/training/permissions/requesting.html

I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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