1

私は民間企業の Android アプリケーションを構築しています。私の目標は、利用可能なアップデートがあることを検出し、ユーザーにダウンロードを提供することです。ユーザーが更新ファイルのダウンロードを選択すると、Android アプリのインストール プロンプトが表示されます。

問題は、apkファイルがダウンロードされていない(空のファイルが作成されている)ため、「パッケージの解析に問題があります」ということです。Android アプリのインストール プロンプトにエラーが表示されます。

コード:

public void downloadfileto(String fileurl, String filename) { 
    String myString; 
    try { 
            FileOutputStream f = new FileOutputStream(filename); 
            try { 
                    URL url = new URL(fileurl); 
                    URLConnection urlConn = url.openConnection(); 
                    InputStream is = urlConn.getInputStream(); 
                    BufferedInputStream bis = new BufferedInputStream(is, 8000); 
                    int current = 0; 
                    while ((current = bis.read()) != -1) { 
                            f.write((byte) current); 
                    } 
            } catch (Exception e) { 
                    myString = e.getMessage(); 
            } 
            f.flush(); 
            f.close(); 
            install(filename);
    } catch (FileNotFoundException e) { 
            e.printStackTrace(); 
    } catch (IOException e) { 
            e.printStackTrace(); 
    } 
} 

protected void install(String fileName) {
    Intent install = new Intent(Intent.ACTION_VIEW);
    install.setDataAndType(Uri.fromFile(new File(fileName)),
            "application/vnd.android.package-archive");
    startActivity(install);
}

関数 downloadfileto は次のように呼び出されます。

downloadfileto("http://some-url/ind.apk", "data/data/my.package.name/app.apk");
4

2 に答える 2

1

ここで助けてくれてありがとう。Webビューでダウンロードをカウントするサーバーでphpスクリプトを開き、ダウンロード、ダウンロードのパスを検出し、アプリケーションをインストールするアクティビティを開始することで問題を解決しました。

ファイルの名前は常に「ind-version.apk」(例: ind-1-0.apk) の形式であり、更新を確認するときに新しい更新のバージョン番号を取得するため、それをエクストラに入れて使用することにしましたファイル名を決定します。

コード:

    WebView myWebView = (WebView) findViewById(R.id.helpview);
    showDialog();
    myWebView.setWebViewClient(new WebViewClient());

    myWebView.loadUrl(url);
    myWebView.getSettings().setJavaScriptEnabled(false);

    myWebView.setWebViewClient(new WebViewClient() {
         @Override  
         public void onPageFinished(WebView view, String url) {
             super.onPageFinished(view, url);
             dismissDialog();
         }  
    });

    myWebView.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(String url, String userAgent,
                String contentDisposition, String mimetype,
                long contentLength) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            startActivity(intent);

            Bundle extras = getIntent().getExtras();
            String v = extras.getString("v");
            v = v.replace(".", "-");
            Log.i("File", v);

            File loc = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
            Log.i("File", loc.toString() + "/ind-" + v + ".apk");

            install(loc.toString() + "/ind-" + v + ".apk");
        }
    });

そしてインストールします:

protected void install(String fileName) {
    Intent install = new Intent(Intent.ACTION_VIEW);
    install.setDataAndType(Uri.fromFile(new File(fileName)),
            "application/vnd.android.package-archive");
    startActivity(install);
}
于 2012-12-09T19:50:53.163 に答える
1

ダウンロードに成功しても、インストーラー プロセスがファイルを読み取ることができないため、APK ファイルをインストールすることはできません。さらに、Chris Stratton が指摘しているように、ハードコーディングされたパスは (Android 4.1 以前では) ずさんで、(Android 4.2 以降では) 壊滅的です。

ダウンロード ロジックに関しては、一度に 1 バイトずつダウンロードするのは、うまく機能しない可能性があります。次のようなことを試してください(File名前付きoutputおよびURL名前付きの場合url):

  HttpURLConnection c=(HttpURLConnection)url.openConnection();

  c.setRequestMethod("GET");
  c.setReadTimeout(15000);
  c.connect();

  FileOutputStream fos=new FileOutputStream(output.getPath());
  BufferedOutputStream out=new BufferedOutputStream(fos);

  try {
    InputStream in=c.getInputStream();
    byte[] buffer=new byte[8192];
    int len=0;

    while ((len=in.read(buffer)) > 0) {
      out.write(buffer, 0, len);
    }

    out.flush();
  }
  finally {
    fos.getFD().sync();
    out.close();
  }
于 2012-12-09T18:31:01.353 に答える