Download And Install apk from a link.
        Posted  
        
            by rayman
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by rayman
        
        
        
        Published on 2010-06-17T14:41:20Z
        Indexed on 
            2010/06/17
            14:43 UTC
        
        
        Read the original article
        Hit count: 587
        
android
|android-sdk
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.
© Stack Overflow or respective owner