Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download And Install apk from a link

Tags:

android

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.

like image 227
rayman Avatar asked Jun 17 '10 14:06

rayman


People also ask

How do I find APK link?

If you want to locate the APK files in your Android phones, you can find the APK for user-installed apps under /data/app/directory while the preinstalled ones are located in /system/app folder and you can access them by using ES File Explorer.


2 Answers

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.

like image 85
CommonsWare Avatar answered Oct 05 '22 15:10

CommonsWare


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

like image 25
Alex Volovoy Avatar answered Oct 05 '22 13:10

Alex Volovoy