Android Upload File Percentage ProgressBar with OKHTTP3

Does anyone know how I can add a progress bar that tells me how many files have been uploaded . This is what I was able to do in the code below, I was able to select a file from my phone and also managed to send it, but the problem is that I am downloading a huge file that I am waiting for not knowing when it will be and so I I need a progress bar that shows me how much has been pushed to the server. This is the code I have, I have tried different implementations but to no avail.

public class MainActivity extends AppCompatActivity {
private Button fileUploadBtn;
protected static String IPADDRESS = "http://10.42.0.1/hugefile/save_file.php";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    fileUploadBtn = (Button) findViewById(R.id.btnFileUpload);
    pickFile();

}

private void pickFile() {

    fileUploadBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new MaterialFilePicker()
                    .withActivity(MainActivity.this)
                    .withRequestCode(10).start();
        }
    });
}

ProgressDialog progress;

@Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
    progress = new ProgressDialog(MainActivity.this);
    progress.setTitle("Uploading File(s)");
    progress.setMessage("Please wait...");
    progress.show();
    if (requestCode == 10 && resultCode == RESULT_OK) {

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

                File f = new File(data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH));

                String content_type = getMimeType(f.getPath());
                String file_path = f.getAbsolutePath();

                OkHttpClient client = new OkHttpClient();
                RequestBody file_body = RequestBody.create(MediaType.parse(content_type), f);
                RequestBody request_body = new MultipartBody.Builder()
                        .setType(MultipartBody.FORM)
                        .addFormDataPart("type", content_type)
                        .addFormDataPart("uploaded_file", file_path.substring(file_path.lastIndexOf("/") + 1), file_body).build();

                Request request = new Request.Builder()
                        .url(IPADDRESS)
                        .post(request_body)
                        .build();
                try {

                    Response response = client.newCall(request).execute();
                    Log.d("Server response", response.body().string());
                    if (!response.isSuccessful()) {
                        throw new IOException("Error : " + response);
                    }
                    progress.dismiss();
                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
        });
        t.start();

    }
}

private String getMimeType(String path) {
    String extension = MimeTypeMap.getFileExtensionFromUrl(path);
    return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);

}

      

}

+3


source to share


2 answers


I was able to solve it with some improvements and here is a link to the solution for those facing the same issue https://github.com/MakuSimz/Android-Multipart-Upload-with-Progress



+1


source


I think your whole solution needs fixing. It is bad practice to pause the user from using a dialog, and boot progress can also be lost when the configuration is changed. And the new topic I'm really too raw these days. Therefore, first of all, I advise you to move the code of the loading process to a Service or IntentService. You can show progress and status in a notification and then alert the user to a dialogue or a nook, etc. Second, there is no direct way to track download progress. Usually the best way is to implement a custom RequestBody, which will notify the bytes loaded through the listener. Refer to Tracking the Progress of Multi-File Uploads with OKHTTP Then you can use broadcasts or the event bus to publish the results.



0


source







All Articles