【问题标题】:How to set a virtual scene image programmatically on android emulator如何在android模拟器上以编程方式设置虚拟场景图像
【发布时间】:2019-11-01 13:51:21
【问题描述】:

我已经为 Flutter 项目编写了一些驱动测试,并且我可以使用 android 模拟器提供的 virtual scene 工具成功测试条码扫描仪功能。

但是,有很多情况需要针对不同的条形码进行测试。我想为每个案例在虚拟场景上设置一个特定的条形码图像。有可能吗?

我发现这张图片的值被放在了~/.android/avd/[emulatorName]/AVD.conf 文件的virtualscene\posters 变量中。

virtualscene\posters=@Variant(\0\0\0\b\0\0\0\x2\0\0\0\b\0w\0\x61\0l\0l\0\0\0\n\xff\xff\xff\xff\0\0\0\n\0t\0\x61\0\x62\0l\0\x65\0\0\0\n\xff\xff\xff\xff)

virtualscene\posters=@Variant(\0\0\0\b\0\0\0\x2\0\0\0\b\0w\0\x61\0l\0l\0\0\0\n\ 0\0\0\\\0/\0U\0s\0\x65\0r\0s\0/\0l\0\x65\0o\0n\0\x61\0r\0\x64\0o\0.\0\x61\0r\0m\0\x65\0r\0o\0/\0\x44\0\x65\0s\0k\0t\0o\0p\0/\0J\0\x61\0m\0\x65\0s\0W\0i\0l\0s\0o\0n\0.\0p\0n\0g\0\0\0\n\0t\0\x61\0\x62\0l\0\x65\0\0\0\n\xff\xff\xff\xff)

【问题讨论】:

    标签: android android-studio flutter


    【解决方案1】:

    您可以将位于$ANDROID_SDK_HOME/emulator/resources/poster.png 的默认(全局)图像替换为您的 poster.png 图像, 也不要通过编辑文件$ANDROID_SDK_HOME/emulator/resources/Toren1BD.posters来更改默认指针。

    【讨论】:

      【解决方案2】:

      您可以将虚拟场景图像设置为指定路径。并在测试时操纵目标图像。 由于 Instrumented 测试在您的(虚拟)设备上运行,因此它无法直接操作主机文件。可以做什么(这是一个丑陋的黑客)是在主机上启动一个服务器,可以从具有主机环回“10.0.2.2”地址的虚拟设备访问该服务器。 该服务器可以操作目标文件。 如果有人有更好的解决方案,请分享!

      这里有一个示例服务器和客户端。

      服务器:

      import java.io.*;
      import java.net.*;
      import java.nio.channels.FileChannel;
      
      public class FileManipulatorServer {
          public static void main(String args[]) {
              int port = 6789;
              FileManipulatorServer server = new FileManipulatorServer( port );
              server.startServer();
          }
      
          // declare a server socket and a client socket for the server
      
          private ServerSocket fileManipulatorServer = null;
          private Socket clientSocket = null;
          private int port;
      
          public FileManipulatorServer(int port ) {
              this.port = port;
          }
      
          public void stopServer() {
              System.out.println( "Server cleaning up." );
              System.exit(0);
          }
      
          public void startServer() {
              // Try to open a server socket on the given port
              // Note that we can't choose a port less than 1024 if we are not
              // privileged users (root)
      
              try {
                  fileManipulatorServer = new ServerSocket(port);
              }
              catch (IOException e) {
                  System.out.println(e);
              }
      
              System.out.println( "Waiting for connections. Only one connection is allowed." );
      
              // Create a socket object from the ServerSocket to listen and accept connections.
              // Use FileManipulatorTask to process the connection.
      
              while ( true ) {
                  try {
                      clientSocket = fileManipulatorServer.accept();
                      FileManipulatorTask task = new FileManipulatorTask(clientSocket, this);
                      task.run();
                  }
                  catch (IOException e) {
                      System.out.println(e);
                  }
              }
          }
      }
      
      class FileManipulatorTask {
          private BufferedReader is;
          private PrintStream os;
          private Socket clientSocket;
          private FileManipulatorServer server;
      
          public FileManipulatorTask(Socket clientSocket, FileManipulatorServer server) {
              this.clientSocket = clientSocket;
              this.server = server;
              System.out.println( "Connection established with: " + clientSocket );
              try {
                  is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                  os = new PrintStream(clientSocket.getOutputStream());
              } catch (IOException e) {
                  System.out.println(e);
              }
          }
      
          public void run() {
              String line;
              try {
                  boolean serverStop = false;
      
                  line = is.readLine();
                  System.out.println( "Received " + line );
                  saveImageToPoster(line.trim());
      
                  os.println("OK");
                  os.flush();
      
                  System.out.println( "Connection closed." );
                  is.close();
                  os.close();
                  clientSocket.close();
      
                  if ( serverStop ) server.stopServer();
              } catch (IOException e) {
                  System.out.println(e);
              }
          }
      
          private void saveImageToPoster(String filename) {
      
              try {
                  FileChannel src = new FileInputStream("C:\\fullpathtopostercandidates\\"+filename).getChannel();
                  FileChannel dest = new FileOutputStream("C:\\fullpathtoconfiguredposter\\poster.jpg").getChannel();
                  dest.transferFrom(src, 0, src.size());
              } catch (FileNotFoundException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
      

      客户:

      import java.io.*;
      import java.net.*;
      
      public class FileNameSenderClient {
      
          private String hostname = "10.0.2.2";
          private int port = 6789;
      
          public void sendFileName(String filename) {
      
              Socket clientSocket = null;
              DataOutputStream os = null;
              BufferedReader is = null;
              try {
                  clientSocket = new Socket(hostname, port);
                  os = new DataOutputStream(clientSocket.getOutputStream());
                  is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
              } catch (UnknownHostException e) {
                  System.err.println("Don't know about host: " + hostname);
              } catch (IOException e) {
                  System.err.println("Couldn't get I/O for the connection to: " + hostname);
              }
      
      
              if (clientSocket == null || os == null || is == null) {
                  System.err.println( "Something is wrong. One variable is null." );
                  return;
              }
      
              try {
                  System.out.println("Write to output stream");
                  os.writeBytes( filename +"\n");
                  os.flush();
                  String responseLine = is.readLine();             
                  System.out.println("Server returns: " + responseLine);
                  os.close();
                  is.close();
                  clientSocket.close();
              } catch (UnknownHostException e) {
                  System.err.println("Trying to connect to unknown host: " + e);
              } catch (IOException e) {
                  System.err.println("IOException:  " + e);
              }
          }
      }
      

      像这样使用您的仪器测试中的 FileNameSenderClient。

      @Test
      public void testQRcodeReadingOK()
      {
         FileNameSenderClient c = new FileNameSenderClient();
         c.sendFileName("QRCode.jpg");
      
         //your code that wants to use the image, like the this:
      
          onView(withId(R.id.load_qr_code)).perform(click());
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-11
        • 2011-12-22
        • 2019-06-30
        • 1970-01-01
        • 1970-01-01
        • 2018-11-20
        • 1970-01-01
        • 2020-08-19
        相关资源
        最近更新 更多