简体   繁体   English

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

[英]How to write .apk file on android phone internal storage, so that I can install that app from internal space programatically

I want to install my app on android phone memory, if their is no enough space to installation then only it would install on external storage, but this all do programatically, not using 我想将我的应用程序安装在android手机内存上,如果它们的安装空间不足,则只能将其安装在外部存储设备上,但这一切都是通过编程方式完成的,而不是使用

"android:installLocation= blahblah" in android manifest file. android清单文件中的“ android:installLocation = blahblah”。

so how should I do that? 那我该怎么办呢?

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

Try below code for write .apk file in internal storage: 尝试使用以下代码在内部存储中写入.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();
                    }
                }

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

相关问题 Android如何安装存储在内部存储中的apk - Android how to install apk stored in internal storage 如何从URL下载内部存储中的apk然后在android中自动安装? - How to download apk in internal storage from URL then install automatically in android? 如何将文件写入Android上内部存储的文件夹? - How can I write a file to a folder of the internal storage on Android? 以编程方式从 Android 5.0 - 6.0 中的应用程序内部存储启动 APK 安装 - Programatically launch an APK installation from app internal storage in Android 5.0 - 6.0 如何从Android的内部存储中检索位图文件? - How can I retrieve a bitmap file from internal storage in Android? 如何在android中播放手机内部存储中的音频文件 - how to play audio file from phone internal storage in android 无法将文件写入内部存储 Android - Can't write file to internal storage Android 如何将Android App安装到内部sdcard或如何利用内部存储 - How to install Android App to internal sdcard or how to utilize the internal storage Android Q(10)如何将文件从我的应用程序的内部存储移动到手机的下载目录? - How to move a file from my app's internal storage to the phone's download directory in Android Q (10)? Android:如何将文件写入内部存储 - Android: how to write a file to internal storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM