You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

API 24设备相机三次调用后黑屏故障求助

Fixing Camera Black Screen on API 24 After Multiple Launches

Hey there! Let's tackle this camera black screen issue you're seeing on API 24 (Android 7.0) after 3 successful launches—since it works fine on API 22, the problem likely ties to stricter resource management or permission changes introduced in newer Android versions. Here's what you can check and fix:

1. Properly Recycle Bitmap Resources

API 24+ enforces tighter memory constraints, and failing to clean up Bitmap instances after each camera use can lead to resource exhaustion over time. This is a common cause of black screens after repeated camera calls.

Update your onActivityResult method to recycle old Bitmaps before setting new ones:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap newBitmap = (Bitmap) extras.get("data");

        // Recycle existing Bitmap in ImageView to free memory
        if (ivPhoto.getDrawable() instanceof BitmapDrawable) {
            Bitmap oldBitmap = ((BitmapDrawable) ivPhoto.getDrawable()).getBitmap();
            if (oldBitmap != null && !oldBitmap.isRecycled()) {
                oldBitmap.recycle();
            }
        }

        ivPhoto.setImageBitmap(newBitmap);
    }
}

Also, clean up resources when the Activity is destroyed:

@Override
protected void onDestroy() {
    super.onDestroy();
    // Clear ImageView references to avoid memory leaks
    if (ivPhoto.getDrawable() instanceof BitmapDrawable) {
        Bitmap bitmap = ((BitmapDrawable) ivPhoto.getDrawable()).getBitmap();
        if (bitmap != null && !bitmap.isRecycled()) {
            bitmap.recycle();
        }
    }
    ivPhoto.setImageDrawable(null);
}

2. Use FileProvider for API 24+

Starting from API 24, Android blocks direct file:// URIs from being passed to other apps (like the camera). While your first 3 calls might work due to temporary permissions, this can cause resource leaks or permission issues that build up over time.

Step 1: Register FileProvider in AndroidManifest.xml

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

Step 2: Create file_paths.xml in res/xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-files-path name="camera_photos" path="Pictures" />
</paths>

Step 3: Update Camera Intent to Use FileProvider

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        File photoFile = null;
        try {
            photoFile = createTempImageFile();
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        if (photoFile != null) {
            Uri photoUri = FileProvider.getUriForFile(
                this,
                getPackageName() + ".fileprovider",
                photoFile
            );
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
}

private File createTempImageFile() throws IOException {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
    String fileName = "CAMERA_PHOTO_" + timeStamp;
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    return File.createTempFile(fileName, ".jpg", storageDir);
}

3. Check for Memory Leaks

Repeated camera launches can expose memory leaks (e.g., holding references to Activity or Bitmap objects). Use tools like LeakCanary to detect leaks—if you find any, fix them by removing unnecessary static references or using weak references where appropriate.

4. Verify Camera Permission Handling

While your first 3 calls work, double-check that you're handling runtime permissions correctly for API 23+. Ensure you request CAMERA permission before launching the intent, and handle cases where the user revokes permission between launches.


内容的提问来源于stack exchange,提问作者Woton Sampaio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:28:03