3

I can't build my project because eclipse giving error

Here is button click event who starting download

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        ProgressDialog mProgressDialog;
        mProgressDialog = new ProgressDialog(Test.this);
        mProgressDialog.setMessage("Pobieranie");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setMax(100);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        DownloadFile downloadFile = new DownloadFile();
        downloadFile.execute("http://google.com");
    }
});

and AsyncTask function

private class DownloadFile extends AsyncTask<String, Integer, String> {
    [...]
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog.show(); // Here eclipse tell that "mProgressDialog cannot be resolved"
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);
        mProgressDialog.setProgress(progress[0]); // Here eclipse tell that "mProgressDialog cannot be resolved"
    }
}

So where I should insert code who create my progress dialog?

darkanzali
  • 85
  • 1
  • 1
  • 10
  • Just a note: You are never allowed to modify UI from a second thread - you can only do so from the main thread (UI thread). The second thread has to tell the main thread what to change in the UI. – ADTC Jul 24 '13 at 10:28

3 Answers3

6
 private class DownloadFile extends AsyncTask<String, Integer, String> {
    ProgressDialog  mProgressDialog;
    String fileName=null;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create ProgressBar
         mProgressDialog = new ProgressDialog(Task.this);
        // Set your ProgressBar Title
        mProgressDialog.setTitle("Downloads");
        mProgressDialog.setIcon(R.drawable.dwnload);
        // Set your ProgressBar Message
        mProgressDialog.setMessage("Updating App Version, Please Wait!");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setMax(100);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        // Show ProgressBar
        mProgressDialog.setCancelable(false);
      //  mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.show();
    }

    @Override
    protected String doInBackground(String... sUrl) {
        try {
                    String apkurl = "http://google.com/"+fileName;
                    URL url = new URL(apkurl);
                    HttpURLConnection c = (HttpURLConnection) url
                            .openConnection();
                    c.setRequestMethod("GET");
                    c.setDoOutput(true);
                    c.connect();
                    int fileLength = c.getContentLength();    

                    //File file = new File(PATH);
                    if(file.exists())
                    {
                        file.delete();
                    }
                    file.mkdirs();
                    File outputFile = new File(file, fileName);
        final FileOutputStream fos = new FileOutputStream(outputFile);

                    final InputStream is = c.getInputStream();


            byte data[] = new byte[1024];
            long total = 0;
            int count;
            while ((count = is.read(data)) != -1) {
                total += count;
                // Publish the progress
                publishProgress((int) (total * 100 / fileLength));
                fos.write(data, 0, count);
            }

            // Close connection
            fos.flush();
            fos.close();
            is.close();
            }
    }

    catch (Exception e) {
            // Error Log
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
 }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        mProgressDialog.dismiss();
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);
        // Update the ProgressBar
        mProgressDialog.setProgress(progress[0]);
    }
}
koutuk
  • 832
  • 1
  • 8
  • 17
0

You erroniously create the ProgressDialog within the scope of the OnClickListener.

Try moving the declaration of mProgressDialog to be a private member variable of your Activity/Fragment instead and only instantiate the ProgressDialog from the OnClickListener.

Cheers!

mach
  • 8,315
  • 3
  • 33
  • 51
0

"This code works fine for downloading data from web"

 private class Downloadpdf extends AsyncTask<String, Integer,String[]> {
    ProgressDialog progressDialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog=new ProgressDialog(MainActivity.this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setIndeterminate(false);
        progressDialog.setMax(100);
        progressDialog.setMessage("Downloading file...");
        progressDialog.show();

    }

    @Override
    protected  String[] doInBackground(String... strings) {
        String url = strings[0];
        String filename = strings[1];
        String extstoragedir = Environment.getExternalStorageDirectory().toString();
        File folder = new File(extstoragedir, foldername);
        boolean bool = folder.mkdir();

        File file = new File(folder, filename);
        try {
            boolean state = file.createNewFile();
        } catch (IOException e) {
            Log.i("log", e.getLocalizedMessage());
            e.printStackTrace();
        }
        int count;
        try {
            Log.i("log","file downloader called");
            URL contenturl=new URL(url);
            HttpURLConnection httpURLConnection= (HttpURLConnection) contenturl.openConnection();
            httpURLConnection.connect();
            InputStream inputStream=httpURLConnection.getInputStream();
            FileOutputStream outputStream=new FileOutputStream(file);
            int totalsize=httpURLConnection.getContentLength();
            Log.i("log","length of file" +String.valueOf(totalsize));
            byte[] buffer= new byte[MEGABYTE];
            int bufferlength=0;
            while((count=inputStream.read(buffer))>0){
                Log.i("log","length caught");
                bufferlength+=count;
                publishProgress((int) ( (bufferlength / (float)totalsize)*100));
                outputStream.write(buffer,0,count);
            }
            inputStream.close();
            outputStream.flush();
            outputStream.close();


        } catch (MalformedURLException e) {
            Log.i("loge",e.getLocalizedMessage());
            e.printStackTrace();
        } catch (IOException e) {
            Log.i("loge",e.getLocalizedMessage());
            e.printStackTrace();
        }


        String[] data=new String[]{filename,strings[2]};
        return data;


    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        System.out.println(values[0]); //updates progress value

        progressDialog.setProgress(values[0]);
    }

    @Override
    protected void onPostExecute(String file[]) {
        progressDialog.dismiss();

    }
}
baswaraj
  • 629
  • 7
  • 7