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

后端 未结 2 1664
醉话见心
醉话见心 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:39

    NOTE: Make this conversion on other thread than Main. RxJava could help to achieve this, or Coroutines

    First convert your bitmap to file

    //create a file to write bitmap data
    File f = new File(context.getCacheDir(), filename);
    f.createNewFile();
    
    //Convert bitmap to byte array
    Bitmap bitmap = your bitmap;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 0 /*ignored for PNG*/, bos);
    byte[] bitmapdata = bos.toByteArray();
    
    //write the bytes in file
    FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            fos.write(bitmapdata);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    

    After that create a request with Multipart in order to upload your file

    RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), f);
    MultipartBody.Part body = MultipartBody.Part.createFormData("upload", f.getName(), reqFile);
    

    Your service call should look like this

    interface Service {
        @Multipart
        @POST("/yourEndPoint")
        Call postImage(@Part MultipartBody.Part image);
    }
    

    And then just call your api

    Service service = new Retrofit.Builder().baseUrl("yourBaseUrl").build().create(Service.class);
    Call req = service.postImage(body);
    req.enqueue(new Callback() {
        @Override
        public void onResponse(Call call, Response response) { 
             // Do Something with response
        }
    
        @Override
        public void onFailure(Call call, Throwable t) {
            //failure message
            t.printStackTrace();
        }
    });
    

提交回复
热议问题