Search code examples
androidfile-uploadretrofit2android-bitmapfileoutputstream

Uploading large images using Retrofit 2


Iam using Retrofit 2.3.0 to upload images to a .Net framework server, the uploading of images i working fine .. ubt when i try to upload >5MB images it gives me an error when i logged the request it doesn't read the full file and gives EOF exception .. and i do have to compress the image with about 50% quality which is not very good iam using MultiPart to upload and this is my code snippet

    RequestBody requestFile =
                    RequestBody.create(MediaType.parse("image/jpeg"), file);
            MultipartBody.Part body =
                    MultipartBody.Part.createFormData("image", file.getName(), requestFile);

// add another part within the multipart request
            RequestBody fullName =
                    RequestBody.create(
                            MediaType.parse("multipart/form-data"), image.getDescription());

and this is the okHttp log

                 --------- beginning of main
10-30 14:25:45.330 11236-11402/com.project.peoject D/OkHttp: �Dwm�1l�n�D�RXZX�1[)$M��l�`��7Q���n�ӟ̊e�L،����=�k���*a�~�g��=�U�ɨ@<�x��/���>� ���R��@��z;c�?��Zʖ7��ۮ�0�*r����#ך�����3�J��F{���v�   ��Kǩ�$||

                                                               read: unexpected EOF!

Solution

  • I have solved this issue by compressing the image file using the Compressor library https://github.com/zetbaitsu/Compressor