Waypoint
Waypoint

Reputation: 17753

Android - internal data save getFilesDir()?

I am downloading from web and APK update file with this code:

        URL url = new URL(urls[0]);
        HttpURLConnection connection2 = (HttpURLConnection) url.openConnection();
        connection2.setRequestMethod("GET");
        connection2.setDoOutput(true);
        connection2.connect();
        int lenghtOfFile = connection2.getContentLength();

        String PATH = ctx.getFilesDir()+"/";
        File apkdir=new File (PATH);
        apkdir.mkdirs();
        File newInstall=new File(apkdir, "app.apk");

        InputStream input = new BufferedInputStream(url.openStream());
        OutputStream output = new FileOutputStream(newInstall);

        byte data[] = new byte[1024];

        long total = 0;

        while ((count = input.read(data)) != -1) {
            total += count;
            publishProgress((int)(total*100/lenghtOfFile));
            prog=(int)(total*100/lenghtOfFile);
            output.write(data, 0, count);
        }

        output.flush();
        output.close();
        input.close();
    } catch (Exception e) {}

    return null;

and for opening file I use this:

    Intent install=new Intent(Intent.ACTION_VIEW);
    install.setDataAndType(Uri.fromFile(new File(ctx.getFilesDir()+"/app.apk")), "application/vnd.android.package-archive");

And here comes the problem - I am ablte to store downloaded app into Context.getFilesDir(), but when I prompt installation pricess, logcat says "unable to read androidmanifest.xml. Where do you see the problem? I need to download into internal, not accesible memory by user, because of security reasons.

Upvotes: 1

Views: 10146

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006869

I need to download into internal, not accesible memory by user, because of security reasons.

This is complete nonsense. A user will still be able to get at your downloaded file, particularly if you install it.

Your problem is that the installer cannot read the file. Either store it in external storage, or you will need to use openFileOutput() with MODE_WORLD_READABLE instead of new FileOutputStream to get your OutputStream.

Upvotes: 3

Related Questions