Home > Software engineering >  How to resize image size before send it to server using Retrofit (MultiPartBody.Part)
How to resize image size before send it to server using Retrofit (MultiPartBody.Part)

Time:03-17

I want to compress my captured picture from camera when sending it to server.

Required Content-Type is a multipart/form-data; boundary with File Parameter.

this my code

                    val requestBody = RequestBody.create(MediaType.parse("multipart/form-data;boundary"), photoFile)
                    val multipartBody = MultipartBody.Part.createFormData("file", photoFile.name, requestBody)

                    Log.d(TAG, "Hit OCR KTP With RequestBody ${requestBody.contentType()}")
                    CompositeDisposable().add(
                        Apis.ocrKtp(Constant.Key.API_KEY, multipartBody)
                            .subscribeWith(object : DisposableObserver<Response<KtpResponse>>() {
                                override fun onNext(t: Response<KtpResponse>) {
                                    if (t.body() != null) {
                                        val body = t.body()

                                        if (body!!.result != null) {
                                            val result = body.result

                                            Log.d(TAG, "$result")
                                        }
                                    } else if  (t.errorBody() != null) {
                                        Log.e(TAG, t.errorBody()!!.string())
                                    }
                                }

                                override fun one rror(e: Throwable) {
                                    Log.e(TAG, e.message!!)
                                }

                                override fun onComplete() {
                                    val bundle = Bundle()
                                    bundle.putString(Constant.Key.IMAGE_URI, savedUri.toString())
                                    bundle.putInt(Constant.Key.IDENTITY_TYPE, [email protected])

                                    GeneralHelper.move(this@TakePictureActivity, KtpFormActivity::class.java, bundle, true)

                                }

                            })
                    )
                }

                override fun one rror(exception: ImageCaptureException) {
                    Log.e(TAG, exception.toString())
                }

            }
        )

Now, I'm getting error message :

Quota file exceeded.

and I think, I must compress my image size for sending captured image to server. But i was try to compress and convert to byte[] or base64. Unfortunately, the server can't validate file type.

hmm I'm getting stuck here, can anyone help? any suggest or answer will be appreciate.

Sorry for bad grammar

CodePudding user response:

You can use Compressor library for that. Here is the link: https://github.com/zetbaitsu/Compressor

CodePudding user response:

For image resize use below lib.

com.github.yalantis:ucrop:2.2.6-native
  • Related