tags:

views:

183

answers:

4

Hi!

The problem is this:

I make an internet connection to some url and receive an HttpResponse with an app_example.apk.

Then I want to create a file (an .apk) in the sdcard with this data so that this downloaded application can be installed later.

How can I convert the HttpResponse to an .apk file?

Let's clear some details:

  • I have to get this apk file through an internet connection to my server
  • I don't want to install this applications I receive on the sdcard
  • All of this has to be done in my code, I cannot use android market

I am currently writing to that file.

What I'm doing is converting the HttpResponse to a byte[ ],

then that byte[ ] is written to a file (an .apk) using an ObjectOutputStream.

Like this:

    // byte[] appByteArray - already has the internet response converted in bytes

    try {
    file = new File(Environment.getExternalStorageDirectory()+"/"+appName+".apk");

    file.createNewFile();

    FileOutputStream stream = null;

    stream = new FileOutputStream(file, false);

    ObjectOutputStream objectOut =
        new ObjectOutputStream(new BufferedOutputStream(stream));

    objectOut.writeObject(appByteArray);

    objectOut.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

In the end, the file is created and has the received content.

When I try to install it, through a VIEW intent (using the default installer) I get a parse error saying that it could not find the AndroidManifest.xml.

I think that in some step along the way, the received data is being corrupted.

Do you have another method to solve this?

Many thanks

A: 

An apk file is just a zip file with a different extension, so you can pull the file off of the sdcard and try to unzip it and see if it has everything in it that you would expect.

CaseyB
+1  A: 

This may not be the core problem, but I don't think you want to wrap stream in an ObjectOutputStream, since that is used for object serialization. It could be that it is adding extra data to the file so it can be deserialized with ObjectInputStream.

I would try pulling the apk off of the emulator (or device) and check it's MD5 versus the file on the server to make sure that the bits are being written out correctly.

Erich Douglass
Indeed. Just write `appByteArray` to `stream` -- your `FileOutputStream`.
Christopher
+3  A: 
  1. Don't use an ObjectOutputStream, byte array is serialized as Object, not written as raw data.
  2. Are you sure that you have SD card write permission? android.permission.WRITE_EXTERNAL_STORAGE
  3. Don't write into SD card root directory. Number of files in root dir can be limited. Instead create you app subdirectory on SD CARD.

This code works for me:

    try {
        String filePath = Environment.getExternalStorageDirectory()
                + "/myappdir/" + appName + ".apk";

        File file = new File(filePath);
        file.getParentFile().mkdirs();
        file.createNewFile();

        BufferedOutputStream objectOut = new BufferedOutputStream(
                new FileOutputStream(file));

        objectOut.write(appByteArray);

        objectOut.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
Pavel P
This piece of code made it work! Like Erich Douglass said, ObjectOutputStream could be adding extra data to the file created, and that might have been the problem.Thank you very much.In Portuguese: Obrigado!
Tsimmi
happy to help :-)
Pavel P
+1  A: 
Christopher