私は民間企業の 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");