Android: Uploading a photo in Cloudinary with progress callback in HttpURLConnection

后端 未结 2 1438
Happy的楠姐
Happy的楠姐 2021-01-12 03:51

I\'m trying to modify the open source library of cloudinary so that I can listen to the progress of the upload of my photo. The library class contains a MultipartUtility jav

2条回答
  •  日久生厌
    2021-01-12 04:08

    There is a problem in the use of flush() method and the time you call update callback().

    As you can see from your code every time you read part of the picture, you write it to the output buffer, but that does not mean it's sent to the server, it might be buffered, and then later on write'n to the server.

    You have two options, either call outputStream.flush() after every outputStream.write(), but that will kill the performance of the upload, because you would lose the benefits of buffering.

    Or you could call your updateCallback() after the outputStream.flush() at the end of your method. Because after outputStream.flush() you are certain that the data is on the server, and that progress is over.

    For more info about the flush see this thread What is the purpose of flush() in Java streams?

提交回复
热议问题