Reputation: 23
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
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
Reputation: 4522
use android:installLocation="internalOnly"
in your manifest
tag in manifest file.
Upvotes: 2