简体   繁体   English

Android-下载APK文件并将其保存在手机存储中(不在SD卡中)

[英]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. 考虑到您的问题,我有几种不同的意见,为什么它不起作用。

  1. Your application download apk into its private folder and maybe PackageManager do not have permissions to read this folder. 您的应用程序将apk下载到其专用文件夹中,并且PackageManager可能没有读取此文件夹的权限。 Try to download application onto sdcard and run it. 尝试将应用程序下载到sdcard上并运行它。
  2. Maybe your .apk file is not properly signed. 也许您的.apk文件未正确签名。 Have you signed it with your generated signature or you export file with the default test signature? 您是否使用生成的签名对其进行了签名,或者是否使用默认的测试签名导出了文件?
  3. If you test this on a real device check that files can be installed from other sources (not only from Google Play market) 如果您在真实设备上进行测试,请检查是否可以从其他来源安装文件(不仅限于Google Play市场)
  4. I do not remember precisely but maybe to call PackageManager to install an application you need a permission. 我记不清了,但也许要调用PackageManager来安装您需要许可的应用程序。

创建文件时,请尝试使用Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM