我正在以字符串格式从服务器下载APK,将其转换为字节数组并在SD卡中创建apk文件。没有问题,如果我尝试安装相同的apk它显示警告(解析错误)消息,如
解析包时出现问题。
和logcat
01-13 12:06:51.562:W / PackageParser(4364):无法读取/mnt/sdcard/example.apk的AndroidManifest.xml 01-13 12:06:51.562:W / PackageParser(4364):java.io.FileNotFoundException:AndroidManifest.xml
01-13 12:06:51.562:W / PackageParser(4364):在android.content.res.AssetManager.openXmlAssetNative(原生方法)
01-13 12:06:51.562:W / PackageParser(4364):在android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:486)
01-13 12:06:51.562:W / PackageParser(4364):在android.content.res.AssetManager.openXmlResourceParser(AssetManager.java:454)
01-13 12:06:51.562:W / PackageParser(4364):在android.content.pm.PackageParser.parsePackage(PackageParser.java:401)
.................................................. ...........
01-13 12:06:51.562:W / PackageParser(4364):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-13 12:06:51.562:W / PackageParser(4364):at dalvik.system.NativeStart.main(Native Method)
01-13 12:06:51.562:W / PackageInstaller(4364):解析清单时解析错误。停止安装
任何想法我做错了什么,或任何其他方法来解决这个问题。
答案 0 :(得分:4)
如果您使用的是9级或更高级别的API,我认为最好使用DownloadManager下载您的apk。因此Android将负责为您下载文件。
答案 1 :(得分:0)
这是下载和安装.apk文件的完美工作代码。
public void downloadInstall(String apkurl){
try {
URL url = new URL(apkurl);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = Environment.getExternalStorageDirectory() + "/download/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, "app.apk");
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "app.apk")), "application/vnd.android.package-archive");
startActivity(intent);
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "download error!", Toast.LENGTH_LONG).show();
}
}
试试这段代码我希望这会有用。