Download Apk File from URL - Android App

Android examples for App:APK Download

Description

Download Apk File from URL

Demo Code


//package com.java2s;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;

import java.net.URL;
import android.app.ProgressDialog;
import android.os.Environment;

public class Main {
    public static File getApkFile(String url, ProgressDialog pd) {

        int last = url.lastIndexOf("/");
        File file = new File(Environment.getExternalStorageDirectory(),
                url.substring(last + 1));
        try {// w  w w  .j  a v  a 2  s .co m
            URL u = new URL(url);
            HttpURLConnection con = (HttpURLConnection) u.openConnection();
            con.setRequestMethod("GET");
            con.setConnectTimeout(5000);
            if (con.getResponseCode() == 200) {
                pd.setMax(con.getContentLength());
                int num = 0;
                InputStream is = con.getInputStream();
                FileOutputStream os = new FileOutputStream(file);
                byte[] buffer = new byte[1024];
                int len;
                while ((len = is.read(buffer)) != -1) {
                    os.write(buffer, 0, len);
                    num += len;
                    Thread.sleep(30);
                    pd.setProgress(num);
                }
                os.flush();
                os.close();
                is.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }
}

Related Tutorials