简体   繁体   中英

How to send an image with a Java server to an Android device

I'm trying to send a picture with a Java server, hosted on my computer to an Android device. But when the picture arrives to the phone, it is cut: a small picture can be read (4ko) but with a 43ko picture, just the third of the picture is visible.

Server:

File f = new File("img.jpg");
BufferedImage buffer = ImageIO.read(f); 

ServerSocket server = new ServerSocket(port);
Socket sock = server.accept();

ImageIO.write(buffer,"JPG",sock.getOutputStream()); 

Android:

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

     try {
         StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
         StrictMode.setThreadPolicy(policy);

         Socket sock = new Socket(ip, port);

         Drawable d = new BitmapDrawable(getResources(), BitmapFactory.decodeStream(sock.getInputStream()));

         ImageView iv = findViewById(R.id.iv);

         iv.setImageDrawable(d);

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

}

Use any Image View library to preview Image.Hope it will work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM