Amol Pawar
Amol Pawar

Reputation: 23

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:installLocation= blahblah" in android manifest file.

so how should I do that?

Upvotes: 1

Views: 985

Answers (2)

Sagar Maiyad
Sagar Maiyad

Reputation: 12743

Try below code for write .apk file in internal storage:

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();
                    }
                }

Upvotes: 0

Nitin Misra
Nitin Misra

Reputation: 4522

use android:installLocation="internalOnly" in your manifest tag in manifest file.

Upvotes: 2

Related Questions