MediaStore.EXTRA_VIDEO_QUALITY在Android视频拍摄中失效问题
Hey there, let's tackle this issue you're facing with Google Pixel devices ignoring your MediaStore.EXTRA_VIDEO_QUALITY and size limit parameters. I've run into similar compatibility quirks with OEM camera apps before, so here's a breakdown of why this happens and how to fix it:
Why This Happens
Different Android manufacturers customize their default camera apps, and not all of them strictly honor the extra parameters passed via ACTION_VIDEO_CAPTURE intent. Google's Pixel camera, in particular, prioritizes its own user-facing settings over these intent extras, which is why your parameters work on Samsung but not on Pixel.
Solution 1: Verify Correct Parameter Passing
First, double-check that you're passing the parameters correctly in your intent. Here's a corrected version of your code snippet, including proper handling for Android 10+ storage restrictions:
private static final int REQUEST_VIDEO_CAPTURE = 1; public void takeVideoFromCamera() { Intent videoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); // Set video quality (1 = high, 0 = low) videoIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // Set maximum file size (example: 50MB in bytes) long maxVideoSize = 50 * 1024 * 1024; videoIntent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, maxVideoSize); // Handle output file (compatible with Android 10+) File mediaFile = new File(getExternalFilesDir(Environment.DIRECTORY_MOVIES), "myvideo.mp4"); Uri outputUri; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // Use FileProvider for Android 10+ to avoid storage permission issues outputUri = FileProvider.getUriForFile(this, getPackageName() + ".fileprovider", mediaFile); } else { outputUri = Uri.fromFile(mediaFile); } videoIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri); // Check if any camera app can handle the intent if (videoIntent.resolveActivity(getPackageManager()) != null) { startActivityForResult(videoIntent, REQUEST_VIDEO_CAPTURE); } }
Solution 2: Offer a Camera App Chooser
Since Pixel's default camera ignores these parameters, give users the option to pick a third-party camera app that does support them. Replace the startActivityForResult call with a chooser:
Intent chooserIntent = Intent.createChooser(videoIntent, "Select a Camera App"); if (chooserIntent.resolveActivity(getPackageManager()) != null) { startActivityForResult(chooserIntent, REQUEST_VIDEO_CAPTURE); }
This lets users select apps like Google Camera Go or other third-party cameras that respect the intent extras.
Solution 3: Implement Custom Video Recording (Most Reliable)
If you need strict control over video quality and file size across all devices, the best approach is to implement your own video recording logic instead of relying on the system camera intent. Jetpack CameraX is a great, easy-to-use option for this:
First, add the CameraX dependencies to your build.gradle (Module level):
dependencies { implementation "androidx.camera:camera-core:1.2.3" implementation "androidx.camera:camera-camera2:1.2.3" implementation "androidx.camera:camera-lifecycle:1.2.3" implementation "androidx.camera:camera-video:1.2.3" }
Then, implement the recording logic:
private VideoCapture<Recorder> videoCapture; private void initCameraX() { // Initialize CameraX ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this); cameraProviderFuture.addListener(() -> { try { ProcessCameraProvider cameraProvider = cameraProviderFuture.get(); // Create a Recorder with quality and size settings Recorder recorder = new Recorder.Builder() .setQualitySelector(QualitySelector.from(Quality.HIGH)) .setMaxDurationMillis(30000) // Optional: max recording time in ms .setMaxFileSize(50 * 1024 * 1024) // 50MB max size .build(); videoCapture = VideoCapture.withOutput(recorder); // Bind camera to lifecycle cameraProvider.bindToLifecycle(this, CameraSelector.DEFAULT_BACK_CAMERA, videoCapture); } catch (ExecutionException | InterruptedException e) { e.printStackTrace(); } }, ContextCompat.getMainExecutor(this)); } // Call this method to start recording private void startRecording() { File mediaFile = new File(getExternalFilesDir(Environment.DIRECTORY_MOVIES), "myvideo.mp4"); OutputFileOptions outputOptions = new OutputFileOptions.Builder(mediaFile).build(); videoCapture.startRecording( outputOptions, ContextCompat.getMainExecutor(this), new VideoCapture.OnVideoSavedCallback() { @Override public void onVideoSaved(@NonNull OutputFileResults outputFileResults) { // Handle successful recording } @Override public void onError(int videoCaptureError, @NonNull String message, @Nullable Throwable cause) { // Handle recording error } } ); }
With CameraX, you have full control over all recording parameters, and it works consistently across all modern Android devices, including Pixel.
Final Notes
- Always test storage permissions: On Android 10+, avoid direct access to external storage root directories. Use
getExternalFilesDir()or MediaStore instead. - If you stick with the system camera intent, remember that parameter support is not guaranteed across all OEMs—this is a known limitation of using
ACTION_VIDEO_CAPTURE.
内容的提问来源于stack exchange,提问作者Usman Khan

