从服务器发送图像到 android 并在屏幕上显示图像

sending an image from the server to android and displaying the image on the screen

在我的代码中,我通过 java 套接字将图像从 PC 传输到 phone。一切正常,图片保存到 phone 没有问题。但是问题来了,是否可以在不保存的情况下在屏幕上显示图像?

服务器代码:

     FileInputStream fis = new FileInputStream("D:\fon2.jpg");
     byte [] buffer = new byte[fis.available()];
     fis.read(buffer);
                                   
     ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream());
     oos.writeObject(buffer);
     oos.close();
     System.out.println("Done");

客户代码:

class ClientThread implements Runnable {
        @Override
        public void run() {
            try {
                Socket s = new Socket(HOST, 4444);
                OutputStream out = s.getOutputStream();
                PrintWriter output = new PrintWriter(out);

                output.println("hello");
                output.flush();

                ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
                byte [] buffer = (byte[]) ois.readObject();



               // save image
               @SuppressLint("SdCardPath") FileOutputStream fos =  new FileOutputStream("/sdcard/2.jpg");
               fos.write(buffer);
               fos.close();
               




                output.close();
                out.close();
                s.close();
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }


        }
    } 

这个问题的解决方案:

当您点击图片时,会与服务器建立连接并发送图片

主要代码:

public void image(View view) {
      new Thread(new ClientThread()).start();

      do {
          if (bitmap != null) {
              imageView.setImageBitmap(bitmap);
              break;
          }
      }while (true);

  }


  class ClientThread implements Runnable {
      @Override
      public void run() {
          try {
              Socket s = new Socket(HOST, 4444);
              OutputStream out = s.getOutputStream();
              PrintWriter output = new PrintWriter(out);

              output.println(tour);
              output.flush();

              ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
              buffer = (byte[]) ois.readObject();
              bitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.length);
              bitmap = Bitmap.createScaledBitmap(bitmap,  600 ,600, true);
              Log.d("log", String.valueOf(buffer));


              output.close();
              out.close();
              s.close();

          } catch (IOException | ClassNotFoundException e) {
              e.printStackTrace();
          }


      }
  }
 

布局:

<ImageView
      android:id="@+id/mainImage"
      android:layout_width="150dp"
      android:layout_height="160dp"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:onClick="image"
      android:src="@drawable/ic_gps"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      tools:ignore="UsingOnClickInXml" />

是的,这是可能的。

您的图片在 byte [] buffer = ..

您可以使用 BitmapFactory.decodeByteArray() 创建一个 Bitmap 实例。

之后将位图分配给 ImageView