Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 下载并运行.apk文件时分析错误_Java_Android_Android Intent_Android Download Manager_Download Manager - Fatal编程技术网

Java 下载并运行.apk文件时分析错误

Java 下载并运行.apk文件时分析错误,java,android,android-intent,android-download-manager,download-manager,Java,Android,Android Intent,Android Download Manager,Download Manager,我的应用程序中有一项功能,允许用户下载新版本。 但我的问题是,当我启动应启动安装过程的intent时,有一条消息: 分析错误。分析包时出现问题 这是我的密码 Button button; String urlApp = "example.com/app.apk" String fileName = "app.apk"; DownloadManager downloadManager; @Override protected void onCreate(Bundle savedInstanceS

我的应用程序中有一项功能,允许用户下载新版本。
但我的问题是,当我启动应启动安装过程的intent时,有一条消息:

分析错误。分析包时出现问题

这是我的密码

Button button;
String urlApp = "example.com/app.apk"
String fileName = "app.apk";
DownloadManager downloadManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            File file = new File(Environment.DIRECTORY_DOWNLOADS, fileName);
            if (file.exists()){
                boolean deleted = file.delete();
                Toast.makeText(getApplicationContext(), "Deleted", Toast.LENGTH_SHORT).show();
            }

            downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(urlApp));
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setTitle("Click to update");
            downloadManager.enqueue(request);
            registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

        }
    });

}

private void openFile() {
    Intent install = new Intent(Intent.ACTION_VIEW);
    install.setDataAndType(Uri.fromFile(new File(Environment.DIRECTORY_DOWNLOADS+"/"+fileName)),
            "application/vnd.android.package-archive");
    startActivity(install);
}

BroadcastReceiver onComplete = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        openFile();
    }
};

}
它只适用于.apk文件,.txt文件工作正常。logcat上没有错误。 删除也不起作用,但这不是我的主要问题

提前感谢:D


如果这是一个愚蠢的问题,很抱歉,但这是我第一次使用下载管理器,我之前遇到过同样的问题


请检查您的新应用程序版本的最低API级别以及您正在使用的设备的API级别。

好的,我终于解决了它

而不是:

install.setDataAndType(Uri.fromFile(new File(Environment.DIRECTORY_DOWNLOADS+"/"+fileName)),
        "application/vnd.android.package-archive");
我曾经

    install.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/"+fileName)),
            "application/vnd.android.package-archive");

这太奇怪了。。。Google Play商店为您处理更新。那么,为什么要使用一种不同的、自定义的、不可靠的方法呢?当然我可以做到,但我想自己去做,因为这肯定是一个很好的练习。但如果你想提高效率,那也是浪费时间!我这样做只是为了好玩,所以我的重点不是太多的生产力Minsdk版本是19,targetSdkVersion是23,我的设备也是skdVersion 23。我得到的错误与您的操作相同。但是这个答案并没有真正的帮助,因为在较新版本的Android中,您不能使用
getExternalStorage…
方法。请参见:。