2 u012784504 u012784504 于 2016.02.29 11:13 提问

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)

4个回答

u011249920
u011249920   2016.02.29 11:29

主要是权限问题,解决方法看一下这篇内容http://blog.csdn.net/y150481863/article/details/7867493

u012784504
u012784504 还是不行,我加上 <!-- 加入在SDCard中创建与删除文件的权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <!-- 加入在SDCard中写入数据的权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />,还是显示解析程序包时出现问题
接近 2 年之前 回复
w89h06q22
w89h06q22   2016.02.29 13:06
首先不要急于怀疑代码,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本身就有问题。
也可以尝试下载其他确保没问题的应用试一下,如果还不行再讨论。

u012784504
u012784504 有manifest,然后我去试试下CSDN的,文件是下了,大小也对,但是手动打开也是出现解析程序包出错,这是为什么呢。我直接用网页下载就能打开安装
接近 2 年之前 回复
u012784504
u012784504   2016.02.29 16:37

解决了,敲了一遍慕课网的视频代码= =

qq_24591011
qq_24591011   2016.07.28 11:54

请问知道问题出现在哪了吗

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!