Android拍照应用保存的图片含Open Camera按钮,如何移除该按钮?
Hey there! Let's break down why that unwanted button is ending up in your saved photos and fix it step by step.
First, let's clarify the root cause: That "Open Camera" button is almost certainly part of the system/third-party camera app's UI or your own app's interface—not the actual photo captured by the camera. When you use MediaStore.ACTION_IMAGE_CAPTURE, you're launching an external camera app, and if you're saving a screenshot of your app's screen (instead of the raw captured photo), you'll capture that button along with it.
Here are the two most effective solutions:
Solution 1: Save the Raw Camera Photo (Not a Screen Screenshot)
The fix here is to ensure your app saves the actual photo taken by the camera, not a snapshot of your app's interface. To do this, properly configure the camera intent to save the photo directly to a file you specify.
Update your captureImage() method like this:
private static final int REQUEST_IMAGE_CAPTURE = 1; private String mCurrentPhotoPath; private void captureImage() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File photoFile = null; try { photoFile = createImageFile(); } catch (IOException e) { // Handle file creation errors e.printStackTrace(); return; } if (photoFile != null) { // Use FileProvider to get a content URI (required for Android 7.0+) Uri photoUri = FileProvider.getUriForFile(this, "com.your.package.name.fileprovider", // Replace with your app's FileProvider authority photoFile); // Tell the camera app where to save the raw photo intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); startActivityForResult(intent, REQUEST_IMAGE_CAPTURE); } } // Your existing createImageFile method (ensure it's correctly generating a file) private File createImageFile() throws IOException { String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()); String imageFileName = "JPEG_" + timeStamp + "_"; File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); File image = File.createTempFile( imageFileName, /* Prefix */ ".png", /* Suffix */ storageDir /* Storage directory */ ); mCurrentPhotoPath = image.getAbsolutePath(); return image; }
Then, in onActivityResult (or use registerForActivityResult for AndroidX), you can retrieve the raw photo file, add your GPS coordinates to it, and save it to the gallery. This raw photo will have no UI buttons—just the image captured by the camera.
Solution 2: Build a Custom Camera UI (Full Control)
If you want complete control over the camera interface (no external camera app, no unexpected buttons), use CameraX (Google's recommended camera library for modern Android apps). It's simpler than the old Camera2 API and works across most devices.
Step 1: Add CameraX Dependencies
Add these to your app-level build.gradle file:
dependencies { def camerax_version = "1.3.0" implementation "androidx.camera:camera-core:$camerax_version" implementation "androidx.camera:camera-camera2:$camerax_version" implementation "androidx.camera:camera-lifecycle:$camerax_version" implementation "androidx.camera:camera-view:$camerax_version" }
Step 2: Add a Preview View to Your Layout
<androidx.camera.view.PreviewView android:id="@+id/previewView" android:layout_width="match_parent" android:layout_height="match_parent"/> <!-- Your custom capture button (no "Open Camera" button needed here!) --> <Button android:id="@+id/captureBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal" android:text="Capture Photo"/>
Step 3: Implement CameraX in Your Activity
private PreviewView previewView; private ImageCapture imageCapture; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camera); previewView = findViewById(R.id.previewView); Button captureBtn = findViewById(R.id.captureBtn); // Initialize the camera startCamera(); captureBtn.setOnClickListener(v -> takePhoto()); } private void startCamera() { CameraSelector cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA; // Set up preview Preview preview = new Preview.Builder().build(); preview.setSurfaceProvider(previewView.getSurfaceProvider()); // Set up image capture imageCapture = new ImageCapture.Builder() .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY) .setTargetRotation(getWindowManager().getDefaultDisplay().getRotation()) .build(); // Bind camera to the activity lifecycle CameraX.bindToLifecycle(this, cameraSelector, preview, imageCapture); } private void takePhoto() { if (imageCapture == null) return; File photoFile = null; try { photoFile = createImageFile(); } catch (IOException e) { e.printStackTrace(); return; } ImageCapture.OutputFileOptions outputOptions = new ImageCapture.OutputFileOptions .Builder(photoFile) .build(); // Capture the photo imageCapture.takePicture(outputOptions, ContextCompat.getMainExecutor(this), new ImageCapture.OnImageSavedCallback() { @Override public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) { // Add GPS coordinates to the image and save to gallery addGpsToImageAndSave(photoFile); } @Override public void onError(@NonNull ImageCaptureException exception) { // Handle capture errors exception.printStackTrace(); } }); } private void addGpsToImageAndSave(File photoFile) { // Load the raw photo Bitmap bitmap = BitmapFactory.decodeFile(photoFile.getAbsolutePath()); Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); Canvas canvas = new Canvas(mutableBitmap); // Draw GPS coordinates on the image Paint paint = new Paint(); paint.setColor(Color.WHITE); paint.setTextSize(48f); String gpsText = "GPS: " + getCurrentGpsCoordinates(); // Replace with your GPS logic canvas.drawText(gpsText, 50f, 100f, paint); // Save the modified image to the gallery MediaStore.Images.Media.insertImage(getContentResolver(), mutableBitmap, "Captured Photo", "Photo with GPS coordinates"); // Notify the gallery to refresh sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(photoFile))); } // Replace this with your actual GPS coordinate retrieval logic private String getCurrentGpsCoordinates() { return "12.3456, 78.9012"; }
With CameraX, you build the exact UI you want—no external camera app means no random buttons showing up in your photos.
Quick Recap
- If you're using the system camera intent: Make sure you're saving the raw photo via
EXTRA_OUTPUT, not taking a screenshot of your app's screen. - For full control: Use CameraX to build a custom camera interface where you decide exactly what UI elements are present.
内容的提问来源于stack exchange,提问作者Thanos Infosec




