Sushil
Sushil

Reputation: 533

Unable to download .apk file from remote server in Android

I want to download .apk file from my web server, but I'm getting
java.io.FileNotFoundException: http://xxx/mygames/myapks/demo.apk all the time.

But the file is present in this URL Below is my code, What I'm doing.

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.os.Environment;
import android.util.Log;

public class InstallAPK 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();

            File sdcard = Environment.getExternalStorageDirectory();
            File myDir = new File(sdcard,"Android/data/com.sush19.app/temp");
            myDir.mkdirs();
            File outputFile = new File(myDir, "temp.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.flush();
            fos.close();
            is.close();

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File(sdcard,"Android/data/com.sush19.app/temp/temp.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;
    }
}

On button Click I'm calling as follow

InstallAPK downloadAndInstall = new InstallAPK();
downloadAndInstall.setContext(getApplicationContext());
downloadAndInstall.execute("http://xxx/mygames/myapks/demo.apk");

I tried with text file as well, for that I just uploaded demo.txt file to http://xxx/mygames/myapks/demo.txt I'm getting same Exception, but when I browse this demo.txt file URL it opens in the Browser. What I need to do to download .apk file from the Web server.

I've also Included all the required permissions

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

I'm getting the above Exception at:

InputStream is = c.getInputStream();

Upvotes: 1

Views: 1357

Answers (2)

AmmY
AmmY

Reputation: 1859

It is not your code fault. It is server fault. just try to hit URL address from Browser.

Upvotes: 0

user3189584
user3189584

Reputation: 21

I think your android sdk version is 4.x, maybe you can try to delete the line code c.setDoOutput(true).

Upvotes: 2

Related Questions