1

I have an app that users can upload file. Right now it works. They can upload files and it received in server side. Now I want show upload percent to users while uploading. I used below code to upload files on server.

here is my java code:

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try{
                    new MaterialFilePicker().
                            withActivity(AddActivity.this).
                            withRequestCode(10).
                            start();


                }
                catch (Exception e){
                    return;
                }
            }
        });

and this is my method:

ProgressDialog progress;

@Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
    if (requestCode==10 && resultCode==RESULT_OK){


        progress = new ProgressDialog(AddActivity.this);
        progress.setTitle("Uploading");
        progress.setMessage("please wait...");
        progress.show();


        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();

                file_size = String.valueOf(f.length()/1024);

                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("upload_file",code+file_path.substring(file_path.lastIndexOf(".")),file_body)
                        .build();

                code2 = code+file_path.substring(file_path.lastIndexOf("."));

                Request request = new Request.Builder()
                        .url("http://api.mysite.com/api/uploadfile")
                        .post(request_body)
                        .build();

                try {
                    Response response = client.newCall(request).execute();

                    Log.d("msg",request_body.toString());
                    Log.d("msg",f.getPath().toString());
                    Log.d("msg",getMimeType(f.getPath()));
                    Log.d("msg33",code+file_path.substring(file_path.lastIndexOf(".")));

                    if (!response.isSuccessful()){
                        throw new IOException(("Error:"+response));
                    }
                    progress.dismiss();

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }


        });
        t.start();
    }
}

Please help me to add upload percentage.
Thank you

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
siyavash
  • 168
  • 2
  • 14

1 Answers1

0

You are doing a lot of things wrong here:

  1. Logic responsible for uploading file should not be located in onActivityResult()
  2. You should seperate it into different method
  3. Using thread like that is not a good idea

If you want to upload file or large file you should do it in IntentService class (You will have to create Your own class and extend IntentService), it is something that is meant for things like that. I suggest You to learn about Retrofit2 and OkHttp library cause it will make it a lot of easier.

An example where you can find a one of the ways how to do it is here: Is it possible to show progress bar when upload image via Retrofit 2?

Asking ppl at stackoverflow to write code completly for you is not a good way to improve your skills. Check this link which I gave You.

Good luck

EDIT:

Android Services: http://www.vogella.com/tutorials/AndroidServices/article.html and https://developer.android.com/training/run-background-service/create-service

Retrofit 2: http://www.vogella.com/tutorials/Retrofit/article.html

OkHttp: https://www.journaldev.com/13629/okhttp-android-example-tutorial

Sample: Is it possible to show progress bar when upload image via Retrofit 2?

These link provides all the information You need in order to write this functionality. I don't have videos on YouTube with things like this. I'm never using YouTube to get this kind of information.

DawidJ
  • 1,245
  • 12
  • 19
  • Thanks for your attention and your guidance. But I did not ask to write code for me. I did post my codes here and want to get help to locate progress bar on it. If I knew how should implement that , I never ask for help. I even did not knew that my solution is not a good idea and if I didn't ask and you didn't answer I will remain in wrong way. I really thank you for you respond but I'm not a lazy man. I work on my code in Iran with all limits to access the sources. I just expect RESPECT. Thank you. – siyavash Dec 24 '18 at 06:15
  • I gave You all needed information to write code which You need to write. The only thing left to do is to read it and enter this link. – DawidJ Dec 24 '18 at 22:21
  • Yes, I thank you. And I'm trying to edit my code in order to your answer. Thanks a lot. I just said want respect. – siyavash Dec 25 '18 at 13:19
  • Dawid Jamroży, hello. can you introduce me some sources or youtube video about it? – siyavash Dec 28 '18 at 09:51