Android Retrofit - onProgressUpdate for showing Progress Notification -
I am currently using Retrofit for Android network communication. Is there any way to achieve progress for making a progress progress notification, something that uses Facebook when uploading an image?
Using the case must load the image without full image quality Compression or scaling.
I know how this is possible with an asynchronous, but it will defeat the purpose of using retrofit. Although this may be the way I have to.
This has been withdrawn for answer 1, for a solution compatible with retrofit 2.
I had the same problem and finally managed to do it. I was using Spring Lib and what I showed below worked for spring but since then it was inconsistent because I made a mistake in using it for inputstream. I had transferred all of my APIs to the use of the restoft and uploaded the last, I read the output stream by override to write typed file to update me on the bytes I do Maybe this correction can be done but as I said I made it when I was using Spring, so I used it again. This is the uploading code and it is working on my app, if you want download feedback, you can use @ Streaming and read the input stream.
Progress List
Public Interface Progressive {zero transfered (long number); }
Counts typed file
public class count type file typed file {Personal Stable FINAL END BUFFER_SIZE = 4096; Private Final Progress Listener Listener; Public counting file (string mimetype, file file, progressive listener) {super (mimetype, file); This.listener = Listener; } @ Override throws out the IOException to the OutputStream out {Bite [] buffer = new byte [BUFFER_SIZE]; FileInputStream = New FileInputStream (super.file ()); Long total = 0; Try reading {int; While ((read = in.read (buffer))! = -1) {total + = read; This.listener.transferred (total); Out.written (buffer, 0, reading); }} Finally {in.close (); MyApiService {@Multipart @POST ("/ files") ApiResult uploadFile (@) MyApiService MyApiService Part ("file") typed file resource, @Query ("path") string path); }
SendFileTask
Private class SendFileTask asyncTask and lt; String, integer, upperisult & gt; {Private progressive listener; Private string filePath; Private file type file type; Public SendFileTask (string file path, file type file type) {this.filePath = filePath; This.fileType = fileType; } @ Override protected APRIsult doInBackground (string ... parameter) {file file = new file (file path); TotalSize = file.length (); Logger.d ("upload file size [% d]", totalSize); Listener / New Progressive () {@ Override People Invalid Transfered (Long Number) {Published Progress ((Integer) ((Number / (Float) Total Size) * 100)); }}; String_file type = file type.video.edgeangel (file type)? "Video / MP4": (File type. IMEG.Ageel (file type)? "Image / JPEG": "* / *"); Return MyRestAdapter.getService (). Upload file (new file type typed (_fileType, file, listener), "/ mobile upload"); } @ Override Progress Update on Zero (Integer ... Value) {Logger.d (String.format ("Progress [% d]", Values [0])); // Do something with values [0], its percentage can be easily done by // processbar .set progress (quality [60]) ;;
CountingTypedFile is only a copy of TypedFile but progress including Alicerer.
Comments
Post a Comment