Download apk and start install prompt inside android application
<p>I'm building a private enterprise Android application. My goal is to detect when there is update available and offer user to download it. If user choose to download update file is downloaded and android app install prompt is showed.</p>

<p>I successfully check for update, the problem is that apk file is not downloaded (empty file is created) therefore "There is a problem parsing the package." error is showed in android app install prompt.</p>

<p>Code:</p>

<pre><code>public void downloadfileto(String fileurl, String filename) { 
    String myString; 
    try { 
            FileOutputStream f = new FileOutputStream(filename); 
            try { 
                    URL url = new URL(fileurl); 
                    URLConnection urlConn = url.openConnection(); 
                    InputStream is = urlConn.getInputStream(); 
                    BufferedInputStream bis = new BufferedInputStream(is, 8000); 
                    int current = 0; 
                    while ((current = bis.read()) != -1) { 
                            f.write((byte) current); 
                    } 
            } catch (Exception e) { 
                    myString = e.getMessage(); 
            } 
            f.flush(); 
            f.close(); 
            install(filename);
    } catch (FileNotFoundException e) { 
            e.printStackTrace(); 
    } catch (IOException e) { 
            e.printStackTrace(); 
    } 
} 

protected void install(String fileName) {
    Intent install = new Intent(Intent.ACTION_VIEW);
    install.setDataAndType(Uri.fromFile(new File(fileName)),
            "application/vnd.android.package-archive");
    startActivity(install);
}
</code></pre>

<p>Function downloadfileto is called with:</p>

<pre><code>downloadfileto("http://some-url/ind.apk", "data/data/my.package.name/app.apk");
</code></pre>
