1

Hello everyone

My problem

I have the need to install an apk from an inputstream and the PackageInstaller class is the only one that allows me to do it, but my current code does not work, it gives me the typical error "The application has stopped." And I do not know where I failed. And I do not know where the error occurs.

The code

public static void installPackage(Context context, InputStream inputStream)
    throws IOException {
PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
int sessionId = packageInstaller.createSession(new PackageInstaller
        .SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL));
PackageInstaller.Session session = packageInstaller.openSession(sessionId);

long sizeBytes = 0;

OutputStream out = null;
out = session.openWrite("my_app_session", 0, sizeBytes);

int total = 0;
byte[] buffer = new byte[65536];
int c;
while ((c = inputStream.read(buffer)) != -1) {
    total += c;
    out.write(buffer, 0, c);
}
session.fsync(out);
inputStream.close();
out.close();

// fake intent
IntentSender statusReceiver = null;
Intent intent = new Intent(context, SomeActivity.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
        1337111117, intent, PendingIntent.FLAG_UPDATE_CURRENT);

session.commit(pendingIntent.getIntentSender());
session.close();

}

Information that may be relevant:

  • I use an emulator with the version of android 4.1.2
  • My application requests permission from SuperUsuario (Root), therefore the device has permissions of SuperUsuario

If you know of any way to achieve my goal, I'll be happy to hear it. Thank you so much

PowerApp
  • 33
  • 4

0 Answers0