How to POST a bitmap to a server using Retrofit/Android

后端 未结 2 1666
醉话见心
醉话见心 2021-02-06 11:04

I\'m trying to post a bitmap to a server using Android and Retrofit.

Currently I know how to post a file, but I\'d prefer to send a bitmap dir

2条回答
  •  别那么骄傲
    2021-02-06 11:43

    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    

    you can convert your bitmap into a byte array and then after post this byte array into the server else you can make one tempory file for example

    File file = new File(this.getCacheDir(), filename);
    

    file directly uplaod into the server

提交回复
热议问题