Skip to content

integration_test screenshot Android - FlutterFragmentActivity - PlatformException(Could not copy the pixels, FlutterView is null)  #89683

Closed

Description

@hampsterx

Steps to Reproduce

  1. flutter create test
  2. Change MainActivity to use FlutterFragmentActivity
class MainActivity: FlutterFragmentActivity() {

    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
    }
}
  1. Add integration_test, test_driver as per https://github.com/flutter/flutter/tree/master/packages/integration_test
  2. Add screenshot steps..
 await binding.convertFlutterSurfaceToImage();

    // Trigger a frame.
    await tester.pumpAndSettle();
    await binding.takeScreenshot('screenshot-1');
 flutter drive --driver=test_driver/integration_test.dart --target=integration_test/home_test.dart

Expected results:

Takes screenshot, same as when using FlutterActivity

Actual results:

Failure in method: screenshot
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞═════════════════
The following PlatformException was thrown running a test:
PlatformException(Could not copy the pixels, FlutterView is null,
null, null)

When the exception was thrown, this was the stack:
#0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
#1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:156:18)
<asynchronous suspension>
#2      IOCallbackManager.takeScreenshot (package:integration_test/_callback_io.dart:91:33)
<asynchronous suspension>
#3      IntegrationTestWidgetsFlutterBinding.takeScreenshot (package:integration_test/integration_test.dart:175:39)
<asynchronous suspension>
#4      main.<anonymous closure> (file:///home/tim/AndroidStudioProjects/untitled/integration_test/home_test.dart:40:5)
<asynchronous suspension>
#5      testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:176:15)
<asynchronous suspension>
#6      TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:817:5)
<asynchronous suspension>

flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.5.0, on KDE neon User Edition 5.22 5.8.0-63-generic, locale en_NZ.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2020.3)
[✓] Android Studio
[✓] Connected device (3 available)

• No issues found!

Activity

  1. darshankawar commented on Sep 9, 2021

    @darshankawar
    Member

    @hampsterx
    Thanks for the report. Can you provide the entire MainActivity.kt ? I tried using FlutterFragmentActivity() as you mentioned, but it's giving me bunch of compile errors. With FlutterActivity(), it works properly though.

    Also, please check this similar /related issue for your reference: #83856

  2. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Sep 9, 2021
  3. hampsterx commented on Sep 9, 2021

    @hampsterx
    Author
    
    import androidx.annotation.NonNull;
    import io.flutter.embedding.android.FlutterFragmentActivity
    import io.flutter.embedding.android.FlutterActivity
    import io.flutter.embedding.engine.FlutterEngine
    import io.flutter.plugins.GeneratedPluginRegistrant
    
    class MainActivity: FlutterFragmentActivity() {
    
        override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
            GeneratedPluginRegistrant.registerWith(flutterEngine);
        }
    
    
    }
    

    My project is using local_auth for biometrics which requires the FlutterFragmentActivity hence why I hit this issue.

    cheers~

  4. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Sep 9, 2021
  5. darshankawar commented on Sep 10, 2021

    @darshankawar
    Member

    Thank for the file. I verified this on latest master and stable and see the same behavior. Below exception is thrown:

    console error log
    
    
    D:\FlutterApps\flutter_screenshot_integration_test-main>flutter drive --driver=test_driver/integration_test.dart --target=integration_test/screenshot_test.dart
    Could not load custom device from config index 0: Expected enabled to be a boolean.
    Running "flutter pub get" in flutter_screenshot_integration_test-main...        707ms
    Running Gradle task 'assembleDebug'...                             89.5s
    √  Built build\app\outputs\flutter-apk\app-debug.apk.
    Installing build\app\outputs\flutter-apk\app.apk...                12.4s
    VMServiceFlutterDriver: Connecting to Flutter application at http://127.0.0.1:53233/rWSv5B6ltNs=/
    VMServiceFlutterDriver: Isolate found with number: 1182669862824263
    VMServiceFlutterDriver: Isolate is paused at start.
    VMServiceFlutterDriver: Attempting to resume isolate
    I/flutter (25803): 00:00 +0: screenshot
    VMServiceFlutterDriver: Connected to Flutter application.
    I/flutter (25803): ══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
    I/flutter (25803): The following PlatformException was thrown running a test:
    I/flutter (25803): PlatformException(Could not copy the pixels, FlutterView is null, null, null)
    I/flutter (25803):
    I/flutter (25803): When the exception was thrown, this was the stack:
    I/flutter (25803): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
    I/flutter (25803): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:167:18)
    I/flutter (25803): <asynchronous suspension>
    I/flutter (25803): #2      IOCallbackManager.takeScreenshot (package:integration_test/_callback_io.dart:96:33)
    I/flutter (25803): <asynchronous suspension>
    I/flutter (25803): #3      IntegrationTestWidgetsFlutterBinding.takeScreenshot (package:integration_test/integration_test.dart:175:39)
    I/flutter (25803): <asynchronous suspension>
    I/flutter (25803): #4      main.<anonymous closure> (file:///D:/FlutterApps/flutter_screenshot_integration_test-main/integration_test/screenshot_test.dart:35:5)
    I/flutter (25803): <asynchronous suspension>
    I/flutter (25803): #5      testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:176:15)
    I/flutter (25803): <asynchronous suspension>
    I/flutter (25803): #6      TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:817:5)
    I/flutter (25803): <asynchronous suspension>
    I/flutter (25803):
    I/flutter (25803): The test description was:
    I/flutter (25803):   screenshot
    I/flutter (25803): ════════════════════════════════════════════════════════════════════════════════════════════════════
    I/flutter (25803): 00:01 +0: screenshot [E]
    I/flutter (25803):   Test failed. See exception logs above.
    I/flutter (25803):   The test description was: screenshot
    I/flutter (25803):
    I/flutter (25803): Consider enabling the flag chain-stack-traces to receive more detailed exceptions.
    I/flutter (25803): For example, 'dart test --chain-stack-traces'.
    I/flutter (25803): 00:01 +0 -1: (tearDownAll)
    I/flutter (25803): 00:01 +1 -1: Some tests failed.
    Failure Details:
    Failure in method: screenshot
    ══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞═════════════════
    The following PlatformException was thrown running a test:
    PlatformException(Could not copy the pixels, FlutterView is null,
    null, null)
    
    When the exception was thrown, this was the stack:
    #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
    #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:167:18)
    <asynchronous suspension>
    #2      IOCallbackManager.takeScreenshot (package:integration_test/_callback_io.dart:96:33)
    <asynchronous suspension>
    #3      IntegrationTestWidgetsFlutterBinding.takeScreenshot (package:integration_test/integration_test.dart:175:39)
    <asynchronous suspension>
    #4      main.<anonymous closure> (file:///D:/FlutterApps/flutter_screenshot_integration_test-main/integration_test/screenshot_test.dart:35:5)
    <asynchronous suspension>
    #5      testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:176:15)
    <asynchronous suspension>
    #6      TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:817:5)
    <asynchronous suspension>
    
    The test description was:
      screenshot
    ═════════════════════════════════════════════════════════════════
    
    end of failure 1
    
    
    
    
    D:\FlutterApps\flutter_screenshot_integration_test-main>
    
    
    
    
    flutter doctor -v
    [√] Flutter (Channel master, 2.6.0-1.0.pre.237, on Microsoft Windows [Version 10.0.19042.1165], locale en-US)
        • Flutter version 2.6.0-1.0.pre.237 at D:\FlutterSDK\flutter
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision a54b10254a (2 hours ago), 2021-09-10 07:22:02 +0300
        • Engine revision 57bc7414b8
        • Dart version 2.15.0 (build 2.15.0-92.0.dev)
    
    [√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
        • Android SDK at C:\Users\Win\AppData\Local\Android\sdk
        • Platform android-31, build-tools 31.0.0
        • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
        • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
        • All Android licenses accepted.
    
    [√] Chrome - develop for the web
        • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
    
    [!] Visual Studio - develop for Windows (Visual Studio Community 2019 16.11.2)
        • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
        • Visual Studio Community 2019 version 16.11.31624.102
        X The current Visual Studio installation is incomplete. Please reinstall Visual Studio.
    
    [√] Android Studio (version 2020.3)
        • Android Studio at C:\Program Files\Android\Android Studio
        • Flutter plugin can be installed from:
           https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
           https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    
    [√] VS Code (version 1.60.0)
        • VS Code at C:\Users\Win\AppData\Local\Programs\Microsoft VS Code
        • Flutter extension can be installed from:
           https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
    
    [√] Connected device (4 available)
        • SM A260G (mobile) • 5200763ebcfa861f • android-arm    • Android 8.1.0 (API 27)
        • Windows (desktop) • windows          • windows-x64    • Microsoft Windows [Version 10.0.19042.1165]
        • Chrome (web)      • chrome           • web-javascript • Google Chrome 93.0.4577.63
        • Edge (web)        • edge             • web-javascript • Microsoft Edge 93.0.961.38
    
    ! Doctor found issues in 1 category.
    
    [√] Flutter (Channel stable, 2.5.0, on Microsoft Windows [Version 10.0.19042.1165], locale en-US)
        • Flutter version 2.2.3 at D:\FlutterSDK\flutter
        • Framework revision f4abaa0735 (31 hours ago), 2021-09-07 12:46:11 -0700
        • Engine revision f0826da7ef
        • Dart version 2.14.0
    
    [√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
        • Android SDK at C:\Users\Win\AppData\Local\Android\sdk
        • Platform android-31, build-tools 31.0.0
        • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
        • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
        • All Android licenses accepted.
    
    [√] Chrome - develop for the web
        • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
    
    [X] Visual Studio - develop for Windows
        X Visual Studio not installed; this is necessary for Windows development.
          Download at https://visualstudio.microsoft.com/downloads/.
          Please install the "Desktop development with C++" workload, including all of its default components
    
    [√] Android Studio
        • Android Studio at C:\Program Files\Android\Android Studio
        • Flutter plugin can be installed from:
           https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
           https://plugins.jetbrains.com/plugin/6351-dart
        • android-studio-dir = C:\Program Files\Android\Android Studio
        • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    
    [√] Connected device (5 available)
        • SM A260G (mobile) • 5200763ebcfa861f • android-arm    • Android 8.1.0 (API 27)
        • SM G975F (mobile) • RZ8M802WY0X      • android-arm64  • Android 11 (API 30)
        • Windows (desktop) • windows          • windows-x64    • Microsoft Windows [Version 10.0.19042.1165]
        • Chrome (web)      • chrome           • web-javascript • Google Chrome 93.0.4577.63
        • Edge (web)        • edge             • web-javascript • Microsoft Edge 92.0.902.84
    
    ! Doctor found issues in 1 category.
    
    C:\Users\Win>
    
    
  6. added
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    f: integration_testThe flutter/packages/integration_test plugin
    platform-androidAndroid applications specifically
    a: platform-viewsEmbedding Android/iOS views in Flutter apps
    and removed
    in triagePresently being triaged by the triage team
    on Sep 10, 2021
  7. BouweCeunen commented on Dec 5, 2021

    @BouweCeunen

    Is there already a solution for this? Also hitting the exact same issue.

  8. hampsterx commented on Dec 6, 2021

    @hampsterx
    Author

    @jmagman @blasten @nturgut

    any thoughts?
    (I see a few prior PR's in this area from ya'll)

  9. 8 remaining items

  10. jmagman commented on Sep 21, 2022

    @jmagman
    Member

    I don't work on this 🙂
    cc @camsim99

  11. francois-robert commented on Jun 14, 2023

    @francois-robert

    Still an issue in flutter 3.10.0, is there a workaround available ?

  12. dkobia commented on Jun 16, 2023

    @dkobia

    Here's what seems to work for me so far:

    • Switch from the deprecated FlutterFragmentActivity to FlutterActivity in MainActivity.kt
    • Use a try/catch with surface->image:
    if (Platform.isAndroid) {
      try {
        await binding.convertFlutterSurfaceToImage();
      } catch (e) {
        // Already converted
      }
    }
    
    await tester.pumpAndSettle();
    await binding.takeScreenshot(screenshotName);
    
  13. hampsterx commented on Jun 17, 2023

    @hampsterx
    Author

    Switch from the deprecated FlutterFragmentActivity

    This is still the recommended way for some packages, eg local_auth and hence why this ticket was created.

    Activity Changes
    Note that local_auth requires the use of a FragmentActivity instead of an Activity. To update your application:
    If you are using FlutterActivity directly, change it to FlutterFragmentActivity in your AndroidManifest.xml.

    #43921

    Seems semi related maybe..

  14. flutter-triage-bot commented on Jul 13, 2023

    @flutter-triage-bot

    This issue is marked P1 but has had no recent status updates.

    The P1 label indicates high-priority issues that are at the top of the work list. This is the highest priority level a bug can have if it isn't affecting a top-tier customer or breaking the build. Bugs marked P1 are generally actively being worked on unless the assignee is dealing with a P0 bug (or another P1 bug). Issues at this level should be resolved in a matter of months and should have monthly updates on GitHub.

    Please consider where this bug really falls in our current priorities, and label it or assign it accordingly. This allows people to have a clearer picture of what work is actually planned. Thanks!

  15. added
    r: fixedIssue is closed as already fixed in a newer version
    on Sep 11, 2023
  16. github-actions commented on Sep 25, 2023

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  17. locked as resolved and limited conversation to collaborators on Sep 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P1High-priority issues at the top of the work lista: platform-viewsEmbedding Android/iOS views in Flutter appsf: integration_testThe flutter/packages/integration_test pluginfound in release: 2.5Found to occur in 2.5found in release: 2.6Found to occur in 2.6has reproducible stepsThe issue has been confirmed reproducible and is ready to work onplatform-androidAndroid applications specificallyr: fixedIssue is closed as already fixed in a newer versionteam-androidOwned by Android platform team

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions