Как показать прогресс на индикаторе выполнения (Retrofit Download) - PullRequest
0 голосов
/ 15 мая 2019

Я использую Retrofit для загрузки файла из Интернета, и я хочу сделать индикатор выполнения, который показывает ход загрузки, но у меня проблемы делая это, я смог сделать уведомление (не идеально), но работает в какой-то момент ..... но похоже на индикатор бесконечности, который не показывает ход загрузки. Как я могу показать прогресс на индикаторе?

private boolean writeResponseBodyToDisk(ResponseBody body, String fileName) {
    try {
        final int progressMax = 463451606;

        Intent activityIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, activityIntent, 0);
        final NotificationCompat.Builder notification = new NotificationCompat.Builder(this, CHANNEL_2_ID)
            .setSmallIcon(R.drawable.ic_cloud_download_black_24dp)
            .setContentTitle("Descargando")
            .setContentText("Archivo descargando")
            .setPriority(NotificationCompat.PRIORITY_LOW)
            .setOngoing(true)
            .setContentIntent(contentIntent)
            .setProgress(progressMax, 0, true);

        notificationManager.notify(2, notification.build());

        // todo change the file location/name according to your needs
        File futureStudioIconFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName);

        InputStream inputStream = null;
        OutputStream outputStream = null;

        try {
            byte[] fileReader = new byte[4096];
            final long fileSize = body.contentLength();
            long fileSizeDownloaded = 0;

            inputStream = body.byteStream();
            outputStream = new FileOutputStream(futureStudioIconFile);

            while (true) {
                int read = inputStream.read(fileReader);

                if (read == -1) {
                    break;
                }

                outputStream.write(fileReader, 0, read);

                fileSizeDownloaded += read;
                Log.e(TAG, "Archivo " + fileSizeDownloaded + " de " + fileSize);

                new Thread(new Runnable() {
                    @Override
                    public void run() {


                        for (int fileSizeDownloaded = 0; fileSize <= progressMax; fileSizeDownloaded++) {

                        }
                        notification.setContentText("Completado")
                            .setProgress(0, 0, false)
                            .setOngoing(false);
                        notificationManager.notify(2, notification.build());


                    }
                }).start();


            }

            outputStream.flush();
            return true;
        } catch (IOException e) {
            return false;
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }

            if (outputStream != null) {
                outputStream.close();
            }
        }
    } catch (IOException e) {
        return false;
    }
}

1 Ответ

0 голосов
/ 23 мая 2019

У рецептов OkHttp на Github есть пример того, как это сделать. https://github.com/square/okhttp/blob/master/samples/guide/src/main/java/okhttp3/recipes/Progress.java

...