[英]Android - Download and save APK files in Phone Storage (Not in SD card)
I was trying to download one APK file from my server and save it in Phone Storage . 我试图从服务器下载一个APK文件并将其保存在“手机存储”中 。
The following is the code that I used: 以下是我使用的代码:
String apkLink = "http://10.1.20.53/files/test.apk";
URL url = new URL(apkLink);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
context.getDir("test.apk", Context.MODE_WORLD_WRITEABLE);
FileOutputStream fos = context.openFileOutput("app_test.apk", Context.MODE_WORLD_WRITEABLE);
InputStream is = c.getInputStream();
fileSize = c.getContentLength();
byte[] buffer1 = new byte[1024];
int len1 = 0;
int total = 0;
while ((len1 = is.read(buffer1)) != -1) {
total += len1;
percentage = (int)(total*100/fileSize);
publishProgress();
fos.write(buffer1, 0, len1);
}
fos.close();
is.close();
The same code is working fine for XML files. 相同的代码可以很好地处理XML文件。 (When i'm downloading XML files to phone memory) .
(当我将XML文件下载到手机内存时)。
Can someone help me with this? 有人可以帮我弄这个吗?
Thank you 谢谢
Actually I figured out the reason, 其实我知道原因了
context.getDir("test.apk", Context.MODE_WORLD_WRITEABLE); context.getDir(“ test.apk”,Context.MODE_WORLD_WRITEABLE); code line creates a directory called "data/data/package_name/app_test.apk"
代码行创建一个名为“ data / data / package_name / app_test.apk”的目录
But FileOutputStream fos = context.openFileOutput("app_test.apk"); 但是FileOutputStream fos = context.openFileOutput(“ app_test.apk”); creates a directory called data/data/package_name/files/app_test.apk
创建一个名为data / data / package_name / files / app_test.apk的目录
And I was trying to read the file from data/data/package_name/app_test.apk directory. 我试图从data / data / package_name / app_test.apk目录中读取文件。
Anyway, Now I am going to execute this file with this code, 无论如何,现在我将使用以下代码执行该文件,
File f = new File(getFilesDir()+ "/app_test.apk");
intent.setDataAndType(Uri.fromFile(f) , "application/vnd.android.package-archive");
startActivity(intent);
But it gives me a parse error message . 但这给了我一个解析错误信息。
Any one can think of a solution? 谁能想到一种解决方案?
Thank you. 谢谢。
I've answered to the similar question here . 我已经在这里回答了类似的问题。 Considering your problem I have several different opinions why it is not working.
考虑到您的问题,我有几种不同的意见,为什么它不起作用。
创建文件时,请尝试使用Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.