我正在尝试从应用程序下载安装 .apk文件。以下是3种不同的情况:
1。从Eclipse到平板电脑,通过adb > 运行应用> 没问题 2。来自Eclipse > Sign&导出> 通过USB将.apk文件传输到平板电脑> 安装&运行应用> 没问题 3。来自Eclipse > Sign&导出> (来自2的相同文件。)将.apk文件上传到服务器> 从应用程序> 下载.apk文件安装> “解析包”
时出现问题下载应用程序的代码:
private class DownloadFile extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... sUrl) {
try {
URL url = new URL(sUrl[0]);
URLConnection connection = url.openConnection();
connection.connect();
// this will be useful so that you can show a typical 0-100% progress bar
int fileLength = connection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory() + File.separator + "Download" + File.separator + "Design102.apk");
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
下载后,为什么我收到“解析程序包时出现问题”同一个.apk文件?
答案 0 :(得分:1)
以下小修改解决了我的问题:
while ((count = input.read(data)) > 0)