如何在Android手机内部存储上编写.apk文件,以便我可以以编程方式从内部空间安装该应用程序

时间:2014-12-19 05:30:26

标签: android

我想在android手机内存上安装我的应用程序,如果它们没有足够的空间来安装那么只有它会安装在外部存储器上,但这一切都以编程方式进行,而不是使用

在android清单文件中

“android:installLocation = blahblah”。

那我该怎么办呢?

2 个答案:

答案 0 :(得分:2)

在清单文件的android:installLocation="internalOnly"标记中使用manifest

答案 1 :(得分:0)

在内部存储中尝试使用以下代码写入.apk文件:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
ResolveInfo resolveInfo = getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
PackageManager packageManager = context.getPackageManager();
String appname = resolveInfo .loadLabel(packageManager);

File apkfile = new File(
                        resolveInfo .activityInfo.applicationInfo.publicSourceDir);
                File f = new File(Yourpath);
                if (f.exists() && f.isDirectory()) {
                    try {
                        File outputFile = new File(f, appname  + ".apk");
                        outputFile.createNewFile();
                        InputStream is = new FileInputStream(apkfile);
                        OutputStream out = new FileOutputStream(outputFile);

                        byte[] buf = new byte[1024];
                        int len;
                        while ((len = is.read(buf)) > 0) {
                            out.write(buf, 0, len);
                        }
                        is.close();
                        out.close();

                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                } else {
                    try {
                        f.mkdirs();
                        File outputFile = new File(f, appname   + ".apk");
                        outputFile.createNewFile();
                        InputStream is = new FileInputStream(apkfile);
                        OutputStream out = new FileOutputStream(outputFile);

                        byte[] buf = new byte[1024];
                        int len;
                        while ((len = is.read(buf)) > 0) {
                            out.write(buf, 0, len);
                        }
                        is.close();
                        out.close();

                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }