开发者

Convert HttpResponse to an .apk file

开发者 https://www.devze.com 2022-12-15 15:50 出处:网络
The problem is this: I make an internet connection to some url and receive an HttpResponse with an app_example.apk.

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) {
 开发者_Go百科       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


  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();
    }


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.


Take a look at Pavel P's answer.

Also, I would note that your idea of installing the APK using the VIEW intent action does work, as I have tested this technique in the past.

However, unless the user has explicitly gone into Settings → Applications and selected "Allow non-Market applications", your installation will fail and the user will just see a screen telling them that for security reasons the installation has been blocked.

Basically you really need to rely on having fairly tech-savvy users who are willing to overlook a scary security warning and go and disable that setting.

0

精彩评论

暂无评论...
验证码 换一张
取 消