tags:

views:

129

answers:

2

Hi, I`am trying to download and install an apk from some link,

but for some reason i get an exception.

I have one method downloadfile() which downloading the file and a call

to and installFile() method, which supposed to install it in the device.

some code:

public void downloadFile()
{
    String fileName = "someApplication.apk";
    MsgProxyLogger.debug(TAG, "TAG:Starting to download");
    try
    {

        URL u = new URL(
                "http://10.122.233.22/test/someApplication.apk");

        try
        {
            HttpURLConnection c = (HttpURLConnection) u.openConnection();

            try
            {
                c.setRequestMethod("GET");
                c.setDoOutput(true);

                try
                {
                    c.connect();


                    FileOutputStream f = context.openFileOutput(fileName,
                            context.MODE_WORLD_READABLE);

                    try
                    {
                        InputStream in = c.getInputStream();

                        byte[] buffer = new byte[1024];
                        int len1 = 0;
                        int totsize = 0;
                        try
                        {
                            while ((len1 = in.read(buffer)) > 0)
                            {
                                totsize += len1;
                                f.write(buffer, 0, len1);// .write(buffer);
                            }
                        } catch (IOException e)
                        {
                            e.printStackTrace();
                        }
                        f.close();
                        MsgProxyLogger.debug(TAG, TAG
                                + ":Saved file with name: " + fileName);

                                    InstallFile(fileName);


                    } catch (IOException e)
                    {
                        e.printStackTrace();
                    }

                } catch (IOException e)
                {
                    e.printStackTrace();
                }

            } catch (ProtocolException e)
            {
                e.printStackTrace();
            }
        } catch (IOException e)
        {
            e.printStackTrace();
        }

    } catch (MalformedURLException e)
    {
        e.printStackTrace();
    }
}

and this is the install file method:

 private void InstallFile(String fileName)
{
    MsgProxyLogger.debug(TAG, TAG + ":Installing file  " + fileName);

    String src = String.format(
            "file:///data/data/com.test/files/",
            fileName);

    Uri mPackageURI = Uri.parse(src);
    PackageManager pm = context.getPackageManager();

    int installFlags = 0;
    try
    {
        PackageInfo pi = pm.getPackageInfo("com.mirs.agentcore.msgproxy",
                PackageManager.GET_UNINSTALLED_PACKAGES);
        if (pi != null)
        {
            MsgProxyLogger.debug(TAG, TAG + ":replacing  " + fileName);

            installFlags |= PackageManager.REPLACE_EXISTING_PACKAGE;
        }
    } catch (NameNotFoundException e)
    {
    }

    try
    {
        // PackageInstallObserver observer = new PackageInstallObserver();
        pm.installPackage(mPackageURI);
    } catch (SecurityException e)
    {
                       //!!!!!!!!!!!!!here i get an security exception!!!!!!!!!!!
        MsgProxyLogger.debug(TAG, TAG + ":not permission?  " + fileName);
    }

this is the exception details: "Neither user 10057 nor current process has android.permission.INSTALL_PACKAGES".

and i have set in my main app that permission in the manifest.

anyone has any idea?

thanks,

ray.

+2  A: 

You need add that permission to manifest http://developer.android.com/reference/android/Manifest.permission.html

Alex Volovoy
What permission?
rayman
INSTALL_PACKAGES... a quick ctrl-f/cmd-f solves that quite quickly.
Lee
+1  A: 

You cannot install APKs that way -- only applications that are part of the system firmware can do that.

You should be able to use an ACTION_VIEW Intent, with a MIME type of application/vnd.android.package-archive and a Uri pointing to your file. Note that this may not work on devices that do not have "allow non-Market installs" checked.

CommonsWare
Is the download and save proccess is also wrong?is there any way of saving the file not on the SDCARD? or mybe do something like save,install,delete ? Could you please past some example of this solution? Thanks.
rayman
Ive managed to do that, thanks! but general question: in which line in my question i coudlnt use(unless the app is in the firmware) ?
rayman
@rayman: You cannot call `installPackage()`. But you knew that already, since that is where your `SecurityException` was, so I am guessing I did not understand your follow-up question.
CommonsWare
I didnt know what was the reason, why the system does throws me an SecurityException.. anyway.. thanks.
rayman
Tell me Mark, do you know if there is any way of controling the confirm_installtion pop-up screen to be displayed prior to the download, as happens in the Market?
rayman
This really isn't the right answer - the answer is in the exception. You can install from within the application just fine, providing the application has the INSTALL_PACKAGES permission in the manifest.
Lee
@Lee: do you have any evidence that `INSTALL_PACKAGES` can be held by applications other than those signed by the firmware signing key?
CommonsWare
Sure, Swype does it.
Lee