Android - Download and save APK files in Phone Storage (Not in SD card)
<p>I was trying to <strong>download one APK file from my server and save it in Phone Storage</strong>. </p>

<p>The following is the code that I used:</p>

<pre><code>            String apkLink = "http://10.1.20.53/files/test.apk";
            URL url = new URL(apkLink);
            HttpURLConnection c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            context.getDir("test.apk", Context.MODE_WORLD_WRITEABLE);
            FileOutputStream fos = context.openFileOutput("app_test.apk", Context.MODE_WORLD_WRITEABLE);

            InputStream is = c.getInputStream();
            fileSize = c.getContentLength();
            byte[] buffer1 = new byte[1024];
            int len1 = 0;
            int total = 0;
            while ((len1 = is.read(buffer1)) != -1) {
                total += len1;
                percentage = (int)(total*100/fileSize);
                publishProgress();
                fos.write(buffer1, 0, len1);
            }
            fos.close();
            is.close();
</code></pre>

<p>The same code is working fine for XML files. (When i'm downloading XML files to phone memory) . </p>

<p>Can someone help me with this? </p>

<p>Thank you</p>

<hr>

<p>Actually I figured out the reason, </p>

<p>context.getDir("test.apk", Context.MODE_WORLD_WRITEABLE); code line creates a directory called "data/data/package_name/app_test.apk"</p>

<p>But FileOutputStream fos = context.openFileOutput("app_test.apk"); creates a directory called data/data/package_name/files/app_test.apk</p>

<p>And I was trying to read the file from data/data/package_name/app_test.apk directory.</p>

<p>Anyway, Now I am going to execute this file with this code, </p>

<pre><code>File f = new File(getFilesDir()+ "/app_test.apk");

intent.setDataAndType(Uri.fromFile(f) , "application/vnd.android.package-archive");
startActivity(intent);
</code></pre>

<p>But it gives me a parse error message . </p>

<p>Any one can think of a solution? </p>

<p>Thank you. </p>
