Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7010473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:59:43+00:00 2026-05-27T21:59:43+00:00

i got some problem to show correctly a ProgressDialog during an image upload. I’ve

  • 0

i got some problem to show correctly a ProgressDialog during an image upload.
I’ve made the HTTPRequest in a AsynkTask and i update the progress bar using doInBackground() and progressUpdate() method, but something’s wrong. When i start the Task from the main activity the ProgressDialog does’t start but it shows itself only after the end of the upload.

Here is the code of the Task:

public class HTTPReqPostTask extends AsyncTask<Void, Integer, String>
{
private funzioni func = new funzioni();
private static final String TAG = "MotoRnUpload";
private ProgressBar progress;
private final Foto img;
private final Album al;
private final URL endpoint;
private String result;
private byte[] bytes;
private ProgressDialog dialog;
private Activity parent;

public HTTPReqPostTask(Activity act,final Foto f,final Album a,final URL u){
    parent = act;
    img = f;
    al = a;
    endpoint = u;
    result = null;
    bytes = func.imgToBytes2(img);        
}

@Override
protected void onPreExecute() {
    dialog = new ProgressDialog(parent);
    dialog.setMessage("Uploading...");
    dialog.setIndeterminate(false);
    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    dialog.setProgress(0);        
    dialog.show();        
}

/*
 * 
 */
@Override
protected String doInBackground(final Void... args)
{
final String CrLf = "\r\n";

int i=0,w,h; 
HttpURLConnection urlc = null;
DataOutputStream printout;
Bitmap b_img = BitmapFactory.decodeFile((img.getFile()).getAbsolutePath());

w = b_img.getWidth();
h = b_img.getHeight();

if(img.getRotationL() == 1 || img.getRotationR() == 1){
        int dim;
        dim = w;
        w = h;
        h = dim;
}

String message1="",message2="",message3="",message4="",message5="",message6="",message7="",message8="",
                message9="",message10="",message11="",message12="",message14="";
try
{
        urlc = (HttpURLConnection) endpoint.openConnection();
        try
        {
                urlc.setRequestMethod("POST");
                urlc.setRequestProperty("Connection", "Keep-Alive");
        } catch (ProtocolException e)
                {
                    Log.e(TAG, e.getMessage(),e.fillInStackTrace());
                }
        urlc.setDoOutput(true);
        urlc.setDoInput(true);
        urlc.setUseCaches(false);
        urlc.setAllowUserInteraction(false);

        message1 += "-----------------------------4664151417711" + CrLf;
        message1 += "Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"test"+bytes.length+".jpg\"" + CrLf;
        message1 += "Content-Type: image/jpeg" + CrLf;
        message1 += CrLf;
        // the image is sent between the messages in the multipart message.
        message2 += CrLf + "-----------------------------4664151417711--" + CrLf;
        //dati normali
        message3 += "-----------------------------4664151417711" + CrLf;
        message3 += "Content-Disposition: form-data; name=\"nomeFoto\"" + CrLf +CrLf;
        //
        message4 += "-----------------------------4664151417711" + CrLf;
        message4 += "Content-Disposition: form-data; name=\"idCat\"" + CrLf +CrLf;
        //
        message5 += "-----------------------------4664151417711" + CrLf;
        message5 += "Content-Disposition: form-data; name=\"idAlbum\"" + CrLf +CrLf;
        //
        message6 += "-----------------------------4664151417711" + CrLf;
        message6 += "Content-Disposition: form-data; name=\"nomeAlbum\"" + CrLf +CrLf;
        //
        message7 += "-----------------------------4664151417711" + CrLf;
        message7 += "Content-Disposition: form-data; name=\"idUser\"" + CrLf +CrLf;
        //
        message8 += "-----------------------------4664151417711" + CrLf;
        message8 += "Content-Disposition: form-data; name=\"isPrivato\"" + CrLf +CrLf;
        //
        message9 += "-----------------------------4664151417711" + CrLf;
        message9 += "Content-Disposition: form-data; name=\"isNew\"" + CrLf +CrLf;
        //
        message10 += "-----------------------------4664151417711" + CrLf;
        message10 += "Content-Disposition: form-data; name=\"width\"" + CrLf +CrLf;
        //
        message11 += "-----------------------------4664151417711" + CrLf;
        message11 += "Content-Disposition: form-data; name=\"height\"" + CrLf +CrLf;
        //
        message12 += "-----------------------------4664151417711" + CrLf;
        message12 += "Content-Disposition: form-data; name=\"gps_lat\"" + CrLf +CrLf;
        //
        message14 += "-----------------------------4664151417711" + CrLf;
        message14 += "Content-Disposition: form-data; name=\"gps_lon\"" + CrLf +CrLf;
        //
        urlc.setRequestProperty("Content-Type", "multipart/form-data; boundary=---------------------------4664151417711");
        // might not need to specify the content-length when sending chunked data.
        //urlc.setRequestProperty("Content-Length", String.valueOf((message1.length() + String.valueOf(message2.length()) + String.valueOf(bytes.length) )));


        printout = new DataOutputStream (urlc.getOutputStream());
        //
        // invia il nome del file
        //
        printout.write(message3.getBytes());
        printout.writeBytes(img.getNome());
        printout.writeBytes(CrLf);
        //
        printout.write(message10.getBytes());
        printout.writeBytes(Integer.toString(w));
        printout.writeBytes(CrLf);
        //
        printout.write(message11.getBytes());
        printout.writeBytes(Integer.toString(h));
        printout.writeBytes(CrLf);
        //
        printout.write(message7.getBytes());
        printout.writeBytes(Integer.toString(img.getIdUser()));
        printout.writeBytes(CrLf);
        //
        printout.write(message9.getBytes());
        printout.writeBytes(Boolean.toString(al.isNew()));
        printout.writeBytes(CrLf);
        //
        if(img.hasGps()){
                printout.write(message12.getBytes());
                printout.writeBytes(img.getGpsLat());
                printout.writeBytes(CrLf);
                //
                printout.write(message14.getBytes());
                printout.writeBytes(img.getGpsLon());
                printout.writeBytes(CrLf);
                //
        }
        //
        if(al.isNew()){
                printout.write(message4.getBytes());
                printout.writeBytes(Integer.toString(al.getIdCat()));
                printout.writeBytes(CrLf);
                //
                printout.write(message6.getBytes());
                printout.writeBytes(al.getNome());
                printout.writeBytes(CrLf);
                //
                printout.write(message8.getBytes());
                printout.writeBytes(Boolean.toString(al.isPrivato()));
                printout.writeBytes(CrLf);
        }else{
                printout.write(message5.getBytes());
                printout.writeBytes(Integer.toString(al.getId()));
                printout.writeBytes(CrLf);
        }
        //
        // invia la foto
        //
        printout.write(message1.getBytes());

        // SEND THE IMAGE
        int size = 1024;    
        do{         
            publishProgress(i);
            if((i+size)>bytes.length){
                size = bytes.length - i;
            }
            printout.write(bytes, i, size);
            i+=size;                
        }while(i<bytes.length);
        publishProgress(bytes.length);
        //
        printout.write(message2.getBytes()); 
        printout.flush ();
        printout.close ();
        Log.i(TAG, "Immagine inviata");            
        //Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
        StringBuffer sb = new StringBuffer();
        String line;
        while ((line = rd.readLine()) != null)
        {
            sb.append(line);
        }   
        rd.close();
        result = sb.toString();
} catch (Exception e)
{
    Log.e(TAG,e.toString(),e.fillInStackTrace());
}
return result;
}

@Override
protected void onProgressUpdate(final Integer... values) {
    dialog.setProgress(values[0]);
}

@Override
protected void onPostExecute(final String res) {
    dialog.dismiss();
}
}

This is the part of code that call the task from the main Activity class:

public void onClick(View view) {
    if(view.getId() == R.id.button1){
        try{
            String result;
            JSONObject info_upload;
            result = new HTTPReqPostTask(MainActivity.this,foto,alb,new               URL(urlUpload)).execute().get();
            info_upload = new JSONObject(result);
            if(info_upload.getString("stato").equals("fail")){
                funzioni.popUp(MainActivity.this,"Errore caricamento foto: "+info_upload.getString("info"));                    
            }else{
                funzioni.popUp(MainActivity.this,"Foto caricata correttamente su MotoRn");
                if(info_upload.getString("stato").equals("create")){
                    alb.setId(info_upload.getInt("id_album"));
                    alb.setIsNew(false);
                }
            }
        }catch(MalformedURLException mue){
            Log.e(TAG,mue.toString(),mue.fillInStackTrace());
            funzioni.popUp(MainActivity.this, mue.getMessage()+" : "+mue.getLocalizedMessage());
        }catch(Exception e){
            Log.e(TAG,e.toString(),e.fillInStackTrace());
            funzioni.popUp(MainActivity.this, e.getMessage()+" : "+e.getLocalizedMessage());
        }
    }else if(view.getId() == R.id.b_open ){
        startActivityForResult(new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI), SELECT_IMAGE_CODE);
    }
}

Thank you for the support 😉

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:59:43+00:00Added an answer on May 27, 2026 at 9:59 pm

    try to pass the real context at that time and i believe you are trying to pass the result of the async task to the String variable “result”. The main thread has to wait until the async task has finished, so its hanging and thus cannot show the dialog.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some problem with Jquery Autocomplete plugin ( http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/ ) I got it
I've got some tabs. http://img196.imageshack.us/img196/7167/tabs.gif And I want to add a rollover effect. Problem
I've got a K-nearest neighbour problem where some of the dimensions are closed loops.
I've got following problem: (c#) There is some class (IRC bot), which has method,
Got a math calculation problem. $a = 34.56 $b = 34.55 $a do some
i`ve got a little problem with LINQ. I read out some information via XML-RPC.
I've got stuck in a problem with gflags when trying to find some memory
I've got a problem on a WebForms application where a user selects some criteria
I've got a quite strange problem here. I'm calling some simple code via Ajax.Updater:
I've got a sproc and some C# calling it. My problem is that both

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.