0

I have been trying to update application from apk. I have tried in different ways (signing the apk and updating the app from the apk in downloads). Nothing was successful. Can anyone suggest a way to do this?

You can look at my code below where I am copying the apk file form the assets folder to internal storage and invoke the apk as an update for that application.

public class UpdateApp extends AsyncTask<String,Void,Void> {
    private Context context;

    public UpdateApp(Context context) {
        this.context=context;
    }

    @Override
    protected Void doInBackground(String... arg0) {
        String PATH = getFilesDir()+"/Download/";
        File file = new File(PATH);
        file.mkdirs();

        File outputFile = new File(file, "App.apk");

        if(outputFile.exists()){
            outputFile.delete();

            //Toast.makeText(MainActivity.this,"wfejhjbweff",Toast.LENGTH_LONG).show();
        }

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(outputFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        AssetManager assetManager = getAssets();

        // Toast.makeText(MainActivity.this,""+s,Toast.LENGTH_LONG).show();
        try {
            InputStream in = assetManager.open("App.apk");
            if(in==null){
            }
            try{
                OutputStream out = fos;
                byte[] buffer = new byte[1024];
                int read = in.read(buffer);
                while (read != -1) {
                    out.write(buffer, 0, read);
                    read = in.read(buffer);
                }
            }
            catch (Exception e){

            }

            OpenNewVersion(PATH);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    void OpenNewVersion(String location) {
        try{
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File(location + "App.apk")), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
        catch (Exception e){

        }
    }
}
TofferJ
  • 4,678
  • 1
  • 37
  • 49
  • 1
    Possible duplicate of [Install Application programmatically on Android](https://stackoverflow.com/questions/4604239/install-application-programmatically-on-android) – Nick Fortescue Jan 31 '18 at 19:33

1 Answers1

0

I came to know the problem in my case is that I was running the application directly from android studio to android device. But later I tried Installing application APK the application then the uploading feature was working. Though I did that, I was not able to avoid installation dialogue.