Download and install an application (apk) from internal memory - no SD card

后端 未结 6 1899
庸人自扰
庸人自扰 2021-01-06 19:35

Greetings and a happy new year to all my fellow programmers.

My code downloads an apk file from a remote server. I need to initiate the installation procedure throug

6条回答
  •  广开言路
    2021-01-06 20:16

    It it caused by android application can not read from another application file if it is written using PRIVATE mode.

    You can do this:

    String fileName = "tmp.apk";
    FileOutputStream fos = openFileOutput(fileName,
            MODE_WORLD_READABLE | MODE_WORLD_WRITEABLE);
    
    // write the .apk content here ... flush() and close()
    
    // Now start the standard instalation window
    File fileLocation = new File(context.getFilesDir(), fileName);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(fileLocation),
                           "application/vnd.android.package-archive");
    context.startActivity(intent);
    

    Be careful though, because that file is now world-visible, and can be seen by any application in the same device, if they know the file location.

提交回复
热议问题