Hi Friends,
If you want to develop android application which is responsible to download .apk file from server(not market app,Enterprise distribution) programmatically and then install it on to the device programmatically, then this post will helps you.......
This can be done using download button and your apk file goes to download and after that your application automatically install downloaded application, you should be apply below logic in download button's click event.
XML code for button:
<Button
android:id="@+id/downloadBtn"
android:layout_width="110dp"
android:layout_height="50dp"
android:layout_marginLeft="810dp"
android:layout_marginTop="395dp"
android:text="Download"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="normal"
android:typeface="serif" />
Following code used to download apk and install on buttons click event;
Button DownloadBTN;
DownloadBTN= (Button)findViewById(R.id.cancelBtn);
DownloadBTN.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
String urlPath="Your APK File url "; //like "http://www.abc.com/Download/abc.apk"
URL url = new URL(urlPath);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(false);
c.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
c.setRequestProperty("Accept","*/*");
c.connect();
String PATH = Environment.getExternalStorageDirectory() + "/download/";
InputStream is = c.getInputStream();
File outputFile = new File(context.getExternalFilesDir
(Environment.DIRECTORY_DOWNLOADS), "abc.apk");
if(outputFile.exists())
{
outputFile.delete();
}
outputFile.setWritable(true);
filepath=outputFile.getPath();
FileOutputStream fos1 = new FileOutputStream(outputFile,true);
try
{
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1)
{
fos1.write(buffer,0,len1);
}
fos1.close();
is.close();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(outputFile), "application/vnd.android.package-archive");
startActivity(intent);
}
catch(Exception ex)
{
}
}
});
If you want to develop android application which is responsible to download .apk file from server(not market app,Enterprise distribution) programmatically and then install it on to the device programmatically, then this post will helps you.......
This can be done using download button and your apk file goes to download and after that your application automatically install downloaded application, you should be apply below logic in download button's click event.
XML code for button:
<Button
android:id="@+id/downloadBtn"
android:layout_width="110dp"
android:layout_height="50dp"
android:layout_marginLeft="810dp"
android:layout_marginTop="395dp"
android:text="Download"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="normal"
android:typeface="serif" />
Following code used to download apk and install on buttons click event;
Button DownloadBTN;
DownloadBTN= (Button)findViewById(R.id.cancelBtn);
DownloadBTN.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
String urlPath="Your APK File url "; //like "http://www.abc.com/Download/abc.apk"
URL url = new URL(urlPath);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(false);
c.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
c.setRequestProperty("Accept","*/*");
c.connect();
String PATH = Environment.getExternalStorageDirectory() + "/download/";
InputStream is = c.getInputStream();
File outputFile = new File(context.getExternalFilesDir
(Environment.DIRECTORY_DOWNLOADS), "abc.apk");
if(outputFile.exists())
{
outputFile.delete();
}
outputFile.setWritable(true);
filepath=outputFile.getPath();
FileOutputStream fos1 = new FileOutputStream(outputFile,true);
try
{
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1)
{
fos1.write(buffer,0,len1);
}
fos1.close();
is.close();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(outputFile), "application/vnd.android.package-archive");
startActivity(intent);
}
catch(Exception ex)
{
}
}
});
How to download and install apk programmatically.I used below example but i got file uri exposed exception.
ReplyDeleteException While install from device storage location:
android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()
Can anyone give suggestion