Search code examples
androiddownloadprogressandroid-annotations

How to do a progress bar to show progress download of a big file with AndroidAnnotations?


I have an Android App and I want to download a big file.

REST API implementation is made with AndroidAnnotations. I need to show a progressbar with the download of a big file using this REST Client (made by AndroidAnnotations).

How I to do that?

Regards


Solution

  • Hello Its to late for answering this question but this will be helpful who are still finding ans with Android-annotation

    You can check your image progress by little bit manipulation of code and here is what i have created my

    Custom converter Class:-

    public class CustomConverter extends FormHttpMessageConverter {
    
    OnProgressListener mOnProgressListener;
    
    public CustomConverter() {
        super();
    
        List<HttpMessageConverter<?>> partConverters = new ArrayList<HttpMessageConverter<?>>();
        partConverters.add(new ByteArrayHttpMessageConverter());
        StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
        stringHttpMessageConverter.setWriteAcceptCharset(false);
        partConverters.add(stringHttpMessageConverter);
        partConverters.add(new ProgressResourceHttpMessageConverter());
        setPartConverters(partConverters);
    }
    

    // public ProgressFormHttpMessageConverter setOnProgressListener(OnProgressListener listener) { // mOnProgressListener = listener; // return this; // }

    class ProgressResourceHttpMessageConverter extends ResourceHttpMessageConverter {
    
        @Override
        protected void writeInternal(Resource resource, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
            InputStream inputStream = resource.getInputStream();
            OutputStream outputStream = outputMessage.getBody();
    
            byte[] buffer = new byte[2048];
            long contentLength = resource.contentLength();
            int byteCount = 0;
            int bytesRead = -1;
            Log.d("<3 <3 <3", "called");
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
                byteCount += bytesRead;
                Log.d("<3 <3 <3 ** ", "progress" + String.valueOf((byteCount * 100) / contentLength));
                if(mOnProgressListener != null) {
                    mOnProgressListener.onProgress(resource, byteCount, (int) contentLength);
                }
            }
            outputStream.flush();
        }
    }
    
    public interface OnProgressListener {
        void onProgress(Resource resource, int downloaded, int downloadSize);
    }
    }
    

    --> you can check your progress with log :)

    Code Usage

    -> Your rest class will be as follow:-

    @Rest(rootUrl = CommonUtils.BASE_URL, converters = {ByteArrayHttpMessageConverter.class,
        CustomConverter.class, StringHttpMessageConverter.class})
    
    public interface CustomRest extends RestClientErrorHandling {
    
    
    @Post(pUrlSignUp)
    String _SignUp(MultiValueMap<String, Object> multiValueMap);
    
    }