0

プログラムで .apk ファイルをダウンロードしてから、そのアクティビティを開始する必要があります。私が使用しているコードは次のとおりです

private String downloadFile(String sourceURL, String destinationPath)
    {
        try {
            URL url = new URL(sourceURL);
            URLConnection connection = url.openConnection();
            connection.connect();

            // download the file
            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream(destinationPath);

            byte data[] = new byte[1024];
            int count;
            while ((count = input.read(data)) > 0) {
                output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();

// ここで編集: ファイルを rw にします。そうしないと、apk ファイルはインストールされません Runtime.getRuntime().exec("chmod 666 " + destinationPath);

            message = "OK";
        }
        catch (MalformedURLException e) {
            message = "Malformed URL error: " + e.getMessage();
        }
        catch (ProtocolException e) {
            message = "Protocol exception error: " + e.getMessage();
        }
        catch (FileNotFoundException e) {
            message = "File not found error: " + e.getMessage();
        }
        catch (IOException e)   {
            e.printStackTrace();
            message = "Failed to download the file : " + e.getMessage();
        }
        return message;
    }

最初にテキスト ファイルに対してメソッドを呼び出し、次に apk ファイルに対してメソッドを呼び出します。したがって、ファイルをローカルで処理するたびに、どういうわけか、何がうまくいかないのかがわかります。このようにして、テキスト ファイルが正しくダウンロードされていることがわかります。しかし、.apk ファイルが破損しています。私はローカルで開発を行っており、DDMS と localhost (IP: 10.0.2.2) にアクセスできるため、原因は上記のコードにあると断言できます。「ダウンロードした」ファイルを DDMS を介して人為的に元の .apk ファイルに置き換えると、その後の処理はすべて問題ありません。さらに、元の .apk ファイルとダウンロードした .apk ファイルを比較すると、byes の違いがあります。私は何を間違っていますか?ありがとう PS: 検索すると、よくある問題ですが、一貫した答えがないことに気付きました。私の場合、純粋にダウンロード方法の問題であると特定しました。

4

1 に答える 1

0

上記のEDITED行の下の回答をご覧ください。実装され、正常に動作し、他の人を助けるかもしれません

于 2012-10-06T16:56:43.040 に答える