我试图从应用程序下载安装.apk文件。以下是3种不同的情况:
1.从Eclipse到平板电脑通过adb>运行应用程序>没有问题 2.从Eclipse> Sign&Export>通过USB传输.apk文件到平板电脑>安装并运行应用程序>没问题 3.从Eclipse> Sign&Export>(来自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文件?
以下小修改修复了我的问题:
while ((count = input.read(data)) > 0)