转换的Htt presponse到.apk文件 [英] Convert HttpResponse to an .apk file

查看:109
本文介绍了转换的Htt presponse到.apk文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是这样的:

我做一个互联网连接到某些URL并接收的Htt presponse与app_example.apk。

然后我想创建一个文件(.apk文件)
在与此数据的SD卡,使得该下载的应用程序
可在以后安装。

我如何能在Htt的presponse转换为一个.apk文件?

让我们澄清一些细节:


  • 我必须通过Internet连接到我的服务器得到这个apk文件

  • 我不想安装这个应用我收到一份关于SD卡

  • 所有这一切都在我的code要做,我不能使用Android市场

我目前正在写该文件。

我在做什么是Htt的presponse转换为一个byte []

那么的byte []写入使用ObjectOutputStream文件(.apk文件)。

这样的:

  //的byte [] appByteArray  - 已经在字节转换的互联网响应    尝试{
    文件=新的文件(Environment.getExternalStorageDirectory()+/+的appName +APK);    file.createNewFile();    的FileOutputStream流= NULL;    流=新的FileOutputStream(文件,FALSE);    ObjectOutputStream的objectOut =
        新的ObjectOutputStream(新的BufferedOutputStream(流));    objectOut.writeObject(appByteArray);    objectOut.close();    }赶上(例外五){
        e.printStackTrace();
    }

在结束时,所创建的文件
并具有接收到的内容。

当我尝试安装它,
 通过视图意图(使用默认安装程序)
我得到一个解析错误说,它无法找到AndroidManifest.xml中。

我认为,在沿途的一些步骤中,接收到的数据被损坏。

你有另一种方法来解决这个问题?

非常感谢


解决方案

  1. 请不要使用一个ObjectOutputStream,字节数组序列化为对象,而不是写成原始数据。

  2. 你确定你有SD卡写入权限? android.permission.WRITE_EXTERNAL_STORAG​​E

  3. 请不要写入SD卡根目录。在根目录的文件的数目可以被限制。相反,在SD卡上创建应用程序,你的子目录。

这code为我工作:

  {尝试
        字符串文件路径= Environment.getExternalStorageDirectory()
                +/ myappdir /+的appName +.apk文件;        档案文件=新的文件(文件路径);
        file.getParentFile()mkdirs()。
        file.createNewFile();        的BufferedOutputStream objectOut =新的BufferedOutputStream(
                新的FileOutputStream(文件));        objectOut.write(appByteArray);        objectOut.close();    }赶上(例外五){
        e.printStackTrace();
    }

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

解决方案

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

这篇关于转换的Htt presponse到.apk文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆