Reputation: 41
Trying to use the following Code to download a remote hosted .apk file.
package com.wireless.wmaa;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;
public class UpdateApp extends AsyncTask<String,Void,Void>{
private Context context;
public void setContext(Context contextf){
context = contextf;
}
@Override
protected Void doInBackground(String... arg0) {
try {
URL url = new URL(arg0[0]);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = "/sdcard/Download/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, "Wireless.apk");
if(outputFile.exists()){
outputFile.delete();
}
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("/sdcard/Download/Wireless.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
context.startActivity(intent);
} catch (Exception e) {
Log.e("UpdateAPP", "Update error! " + e.getMessage());
}
return null;
}}
And Here is the Code to start the Task:
UpdateApp myApp = new UpdateApp();
myApp.setContext(getApplicationContext());
myApp.execute("http://some.domain.com/someSite/some.apk");
Able to connect but when trying to use the InputStream throws a file not found exception. I can browse to the file from the Android Default Browser and I am prompted to download the file which is the same functionality I would like in my App. Here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wireless.wmaa"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.DELETE_PACKAGES" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
>
<activity
android:label="@string/app_name"
android:name=".SelectServer"
android:theme="@android:style/Theme.NoTitleBar"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>"
</activity>
<activity
android:name=".UpdateApp"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar"
android:screenOrientation="landscape" >
<intent-filter>
</intent-filter>
</activity>
<activity
android:name=".WirelessActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar"
android:screenOrientation="landscape" >
<intent-filter>
</intent-filter>
</activity>
</application>
</manifest>
Upvotes: 1
Views: 4266
Reputation: 41
Ended up being an Issue with IIS. Once I moved my .apk to my apache webserver had no issues...
Upvotes: 0
Reputation: 29632
You are wrong at this line File outputFile = new File(file, "Wireless.apk");
just changed it as follows,
File outputFile = new File( PATH + "Wireless.apk");
also change following code
if(outputFile.exists())
{
outputFile.delete();
}
else // Add Else part, it is missing in your code
{
outputFile.createNewFile();
}
Upvotes: 1
Reputation: 6159
Don't use /sdcard/Downloads...this path is different in some devices. You should use:
File file = Environment.getExternalStorageDirectory();
Upvotes: 0