1

我正在从服务器下载一个 Android 应用程序并将其保存到我的应用程序的内部空间,使用:

URL url = new URL(Urls[0]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
File outputFile = new File(getFilesDir(), "Apps");
outputFile.mkdirs();
File file = makeFilename(outputFile, _appName + ".apk");
file.createNewFile();
file.setExecutable(true);
file.setReadable(true, false);
file.setWritable(true, false);
FileOutputStream fout = new FileOutputStream(zipFile);
InputStream in = conn.getInputStream();
byte[] buffer = new byte[1024];
int length = 0;
while ((length = in.read(buffer)) > 0) {
fout.write(buffer, 0, length);
}
fout.close();
in.close();
outputFile.setExecutable(true, false);
outputFile.setReadable(true, false);
outputFile.setWritable(true, false);


private File makeFilename(File base, String name) {
        if (name.indexOf(File.separatorChar) < 0) {
            return new File(base, name);
        }
        throw new IllegalArgumentException("File " + name
                + " contains a path separator");
    }

下载完成后,会出现通知。当我单击此通知时,应安装该应用程序。但实际上,当我点击通知时,会出现“解析包时出错”。我做错了什么?

Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(new File(getFilesDir()+ "/Apps/_appName.apk")),"application/vnd.android.package-archive");
PendingIntent act = PendingIntent.getActivity(MainActivity._activity,0, i, 0);
notification.setLatestEventInfo("TEXT", "TEXT, act);
4

1 回答 1

1

我怀疑您下载的 .apk 文件对您的应用程序本身来说是私有的。

所以检查一下,

而且,当您在应用程序中存储文件时,请确保它具有WORLD_READABLE权限,例如,

Context.MODE_WORLD_READABLE

就像是,

OutputStream myOutputFile = openFileOutput("xxxx.apk", Context.MODE_WORLD_READABLE);
于 2012-07-18T12:49:33.847 回答