[英]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.