Monday, July 1, 2013

APK download internal storage and install

1. First Download
            URL url = new URL(params[0]);
            URLConnection connection = url.openConnection();
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            connection.connect();
            // this will be useful so that you can show a typical 0-100% progress bar
            int fileLength = connection.getContentLength();
     
            File mkDir = mActivity.getDir("assets", Activity.MODE_WORLD_READABLE | Activity.MODE_WORLD_WRITEABLE);
            String path = mkDir.getAbsolutePath();
            Log.d("need", "ApkDownload Location : " + path);
           
         checkMemory(fileLength);  

            // download the file
            input = new BufferedInputStream(url.openStream());
            output = mActivity.openFileOutput(APK_FILE_NAME,
              Activity.MODE_WORLD_READABLE | Activity.MODE_WORLD_WRITEABLE);
//            output = new FileOutputStream(mActivity.getFilesDir().getAbsoluteFile() + File.separator + APK_FILE_NAME);
//            output = new FileOutputStream(SDCardRoot + File.separator + APK_FILE_NAME);
            Log.d("need", "ApkDownload Location : " + path);

            byte data[] = new byte[1024];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {
                total += count;
                // publishing the progress....
                progressPercent =(int) (total * 100 / fileLength);
                publishProgress();
                output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();


2. Second Install
File fileLocation = new File(mActivity.getFilesDir(), ApkFileDownloadTask.APK_FILE_NAME);
         
Log.d("need", "apkFile isFile" + fileLocation.isFile());
Log.d("need", "apkFile getName" + fileLocation.getName());
Log.d("need", "apkFile getAbsolutePath" + fileLocation.getAbsolutePath());
Log.d("need", "apkFile getAbsolutePath" + fileLocation.length());
         
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(fileLocation), "application/vnd.android.package-archive");

startActivity(intent);

No comments:

Post a Comment