gpt4 book ai didi

Android - 内部数据保存 getFilesDir()?

转载 作者:行者123 更新时间:2023-11-29 18:20:57 30 4
gpt4 key购买 nike

我正在使用以下代码从网络和 APK 更新文件下载:

        URL url = new URL(urls[0]);
HttpURLConnection connection2 = (HttpURLConnection) url.openConnection();
connection2.setRequestMethod("GET");
connection2.setDoOutput(true);
connection2.connect();
int lenghtOfFile = connection2.getContentLength();

String PATH = ctx.getFilesDir()+"/";
File apkdir=new File (PATH);
apkdir.mkdirs();
File newInstall=new File(apkdir, "app.apk");

InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(newInstall);

byte data[] = new byte[1024];

long total = 0;

while ((count = input.read(data)) != -1) {
total += count;
publishProgress((int)(total*100/lenghtOfFile));
prog=(int)(total*100/lenghtOfFile);
output.write(data, 0, count);
}

output.flush();
output.close();
input.close();
} catch (Exception e) {}

return null;

打开文件我用这个:

    Intent install=new Intent(Intent.ACTION_VIEW);
install.setDataAndType(Uri.fromFile(new File(ctx.getFilesDir()+"/app.apk")), "application/vnd.android.package-archive");

问题来了——我能够将下载的应用程序存储到 Context.getFilesDir() 中,但是当我提示安装价格时,logcat 说“无法读取 androidmanifest.xml。你在哪里看到问题?我需要出于安全原因,下载到用户无法访问的内部内存中。

最佳答案

I need to download into internal, not accesible memory by user, because of security reasons.

这完全是胡说八道。用户仍然可以访问您下载的文件,尤其是在您安装它的情况下。

您的问题是安装程序无法读取文件。将其存储在外部存储中,或者您需要使用 openFileOutput()MODE_WORLD_READABLE 而不是 new FileOutputStream 来获取您的 OutputStream

关于Android - 内部数据保存 getFilesDir()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5443074/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com