Android:无法安装应用程序保存到内部内存

时间:2012-07-18 12:37:27

标签: android parsing package

我正在从服务器下载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);

1 个答案:

答案 0 :(得分:1)

我怀疑您下载的.apk文件您的应用专用本身。

所以,检查一下,

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

Context.MODE_WORLD_READABLE

类似的东西,

OutputStream myOutputFile = openFileOutput("xxxx.apk", Context.MODE_WORLD_READABLE);