问题描述
- android下载文件打不开
-
我用mars老师的源码,/** * 该函数返回整形 -1:代表下载文件出错 0:代表下载文件成功 1:代表文件已经存在 */ public int downFile(String urlStr, String path, String fileName) { InputStream inputStream = null; try { FileUtils fileUtils = new FileUtils(); if (fileUtils.isFileExist(path + fileName)) { return 1; } else { inputStream = getInputStreamFromUrl(urlStr); File resultFile = fileUtils.write2SDFromInput(path,fileName, inputStream); if (resultFile == null) { return -1; } } } catch (Exception e) { e.printStackTrace(); return -1; } finally { try { inputStream.close(); } catch (Exception e) { e.printStackTrace(); } } return 0; }
返回0,成功下载了,而且目录下看到有文件,但是打开显示解析程序包时出现问题,到底怎么回事
new Thread(new Runnable() { @Override public void run() { HttpDownloader httpDownloader = new HttpDownloader(); int result = httpDownloader.downFile("http://xxxxx.apk", "aaaaaaaa/", "xxxxx.apk");//部分省略 System.out.println(result); } }).start();
logcat
02-29 11:12:40.025 8803-8803/com.android.packageinstaller D/asset: failed to open Zip archive '/storage/emulated/0/aaaaaaaa/xxxxx.apk'
02-29 11:12:40.061 8803-8803/com.android.packageinstaller W/PackageInstaller: Parse error when parsing manifest. Discontinuing installation
02-29 11:12:40.061 8803-8803/com.android.packageinstaller W/PackageParser: Unable to read AndroidManifest.xml of /storage/emulated/0/aaaaaaaa/xxxxx.apk
02-29 11:12:40.061 8803-8803/com.android.packageinstaller W/PackageParser: java.io.FileNotFoundException: AndroidManifest.xml
02-29 11:12:40.061 8803-8803/com.android.packageinstaller W/PackageParser: at android.content.res.AssetManager.openXmlAssetNative(Native Method)
02-29 11:12:40.061 8803-8803/com.android.packageinstaller W/PackageParser: at android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:488)
02-29 11:12:40.061 8803-8803/com.android.packageinstaller W/PackageParser: at android.content.res.AssetManager.openXmlResourceParser(AssetManager.java:456)
02-29 11:12:40.061 8803-8803/com.android.packageinstaller W/PackageParser: at android.content.pm.PackageParser.parsePackage(PackageParser.java:528)
02-29 11:12:40.061 8803-8803/com.android.packageinstaller W/PackageParser: at com.android.packageinstaller.PackageUtil.getPackageInfo(PackageUtil.java:73)
02-29 11:12:40.061 8803-8803/com.android.packageinstaller W/PackageParser: at com.android.packageinstaller.PackageInstallerActivity.onCreate(PackageInstallerActivity.java:465)
02-29 11:12:40.061 8803-8803/com.android.packageinstaller W/PackageParser: at android.app.Activity.performCreate(Activity.java:5231)
02-29 11:12:40.061 8803-8803/com.android.packageinstaller W/PackageParser: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-29 11:12:40.061 8803-8803/com.android.packageinstaller W/PackageParser: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
02-29 11:12:40.061 8803-8803/com.android.packageinstaller W/PackageParser: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
02-29 11:12:40.061 8803-8803/com.android.packageinstaller W/PackageParser: at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-29 11:12:40.061 8803-8803/com.android.packageinstaller W/PackageParser: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-29 11:12:40.061 8803-8803/com.android.packageinstaller W/PackageParser: at android.os.Handler.dispatchMessage(Handler.java:102)
02-29 11:12:40.061 8803-8803/com.android.packageinstaller W/PackageParser: at android.os.Looper.loop(Looper.java:136)
02-29 11:12:40.061 8803-8803/com.android.packageinstaller W/PackageParser: at android.app.ActivityThread.main(ActivityThread.java:5001)
02-29 11:12:40.061 8803-8803/com.android.packageinstaller W/PackageParser: at java.lang.reflect.Method.invokeNative(Native Method)
02-29 11:12:40.061 8803-8803/com.android.packageinstaller W/PackageParser: at java.lang.reflect.Method.invoke(Method.java:515)
02-29 11:12:40.061 8803-8803/com.android.packageinstaller W/PackageParser: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
02-29 11:12:40.061 8803-8803/com.android.packageinstaller W/PackageParser: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
02-29 11:12:40.061 8803-8803/com.android.packageinstaller W/PackageParser: at dalvik.system.NativeStart.main(Native Method)
解决方案
主要是权限问题,解决方法看一下这篇内容http://blog.csdn.net/y150481863/article/details/7867493
解决方案二:
首先不要急于怀疑代码,log里有一行
02-29 11:12:40.061 8803-8803/com.android.packageinstaller W/PackageParser: java.io.FileNotFoundException: AndroidManifest.xml
根据log的上下文,应该是系统调起安装时解析apk出错。
你可以把down下来的apk解压,看一下里面有没有AndroidManifest.xml文件。如果没有,表明下载的apk本身就有问题。
也可以尝试下载其他确保没问题的应用试一下,如果还不行再讨论。
解决方案三:
解决了,敲了一遍慕课网的视频代码= =