Omar
Omar

Reputation: 17

ProgressDialog : Don't Dismissing after showing

    private void copyFile(AppDB apkfile) {
    ProgressDialog pd=new ProgressDialog(this,ProgressDialog.STYLE_SPINNER);
    pd.show(this,"Coping ...",apkfile.name,true,true);
    File f1 = new File(apkfile.location);
    try {
        String fileName = apkfile.name;
        File f2 = new File(Environment.getExternalStorageDirectory().toString() + "/" + "Easy Share");
        f2.mkdirs();
        f2 = new File(f2.getPath() + "/" + fileName + ".apk");
        f2.createNewFile();
        InputStream in = new FileInputStream(f1);
        OutputStream out = new FileOutputStream(f2);
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    } catch (FileNotFoundException ex) {
        Toast.makeText(this, ex.getMessage() + " in the specified directory.", Toast.LENGTH_SHORT);

    } catch (IOException e) {
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT);
    }
    if(pd.isShowing()) {
   pd.dismiss();}

}

Hi everyone when trying to dismiss the progress dialog it wont do that and serarched for many solution but i not its same Problem. please help.

Update: there is Progress Dialog pd's class after running.

Screenshot 1

Upvotes: 0

Views: 71

Answers (3)

Nisar Ahmad
Nisar Ahmad

Reputation: 310

it is working well i will tell you two possibilities

  1. please use it in try block at last as

    try {
        String fileName = apkfile.name;
        File f2 = new File(Environment.getExternalStorageDirectory().toString() + "/" + "Easy Share");
        f2.mkdirs();
        f2 = new File(f2.getPath() + "/" + fileName + ".apk");
        f2.createNewFile();
        InputStream in = new FileInputStream(f1);
        OutputStream out = new FileOutputStream(f2);
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
       if(pd.isShowing()) {
       pd.dismiss();}
        } catch (FileNotFoundException ex) {
            Toast.makeText(this, ex.getMessage() + " in the specified directory.", Toast.LENGTH_SHORT);
       } catch (IOException e) {
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT);
    }
    

    }

second is use in delay

Upvotes: 0

Damien Praca
Damien Praca

Reputation: 3136

The copy should be done in a Thread or a AsyncTask. Show the dialog before executing the AsyncTask and hide in the onPOstExecute

protected void onPostExecute(Long result) {
       if(mProgressDialog.isShowing()) {
          mProgressDialog.dismiss();}
       }  
}

Upvotes: 1

User
User

Reputation: 4211

Try with finally

private void copyFile(AppDB apkfile) {
    ProgressDialog pd=new ProgressDialog(this,ProgressDialog.STYLE_SPINNER);
    pd.show(this,"Coping ...",apkfile.name,true,true);
    File f1 = new File(apkfile.location);
    try {
        String fileName = apkfile.name;
        File f2 = new File(Environment.getExternalStorageDirectory().toString() + "/" + "Easy Share");
        f2.mkdirs();
        f2 = new File(f2.getPath() + "/" + fileName + ".apk");
        f2.createNewFile();
        InputStream in = new FileInputStream(f1);
        OutputStream out = new FileOutputStream(f2);
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    } catch (FileNotFoundException ex) {
        Toast.makeText(this, ex.getMessage() + " in the specified directory.", Toast.LENGTH_SHORT);

    } catch (IOException e) {
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT);
    } finally {
      if(pd.isShowing()) {
        pd.dismiss();
      }
    }
}

Upvotes: 0

Related Questions