title: Android replace application fails to install
body: <p>My appllication implements an auto upgrade capability as the app will not be release to market.
  The apk file is hosted on a webserver. I have implemented part one of the following link</p>
  
  <p><a href="https://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog" title="Use AsyncTask and show the download progress in a dialog">Use AsyncTask and show the download progress in a dialog</a></p>
  
  <p>Now when I run the application the following things occur.</p>
  
  <ol>
  <li><p>If a new version is available then the application prompts the user if they want to upgrade.
  <img src="https://i.stack.imgur.com/eoPdA.png" alt="Upgrade prompt"></p></li>
  <li><p>If the user selects yes then the application downloads the file from the server.
  <img src="https://i.stack.imgur.com/Fth6N.png" alt="Download"></p></li>
  <li>Once completed the app then prompts the user if they want to replace the app
  <img src="https://i.stack.imgur.com/HRnKU.png" alt="Replace prompt"></li>
  <li>If the user hits ok then the application starts to install but fails with the following screen
  <img src="https://i.stack.imgur.com/I3brA.png" alt="enter image description here"></li>
  </ol>
  
  <p>There is no log file information provided as to why this occurs. Please could someone help me understand what could be causing this issue. This is default android code to produce the fianl 2 screens not my code.</p>
  
  <p>This occurs after the following code has been run on the asyncronous task:</p>
  
  <pre><code>    protected void onPostExecute(String result) {
          super.onPostExecute(result);
          // dismiss the progress dialog
          mProgressDialog.dismiss();
          // get the file
          String path = getFilesDir().getAbsolutePath()+ File.separator + getString(R.string.apk_file);
          File file = new File(path);
          // if it exists
          if(file.exists()) {
              // create and start the intent with the new file
              Intent intent = new Intent(Intent.ACTION_VIEW); 
              intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); 
              startActivity(intent);
          } else {
              // display error message 
              Toast.makeText(context, "File was not downloaded", Toast.LENGTH_LONG).show();
          }           
      }
  </code></pre>
  
  <p>If i uninstall the app and use the browser to go the file location the app is downloaded automatically and then I can install it succesfully from the downloads folder.</p>
  
  <p>Could this be a problem that the app is trying to Install its self while the app is currently running? The file is downloaded to the internal memory during the upgrade process and not the SD card location as I cannot guarentee that the users device will have an SD card present.</p>