Reputation: 35
I am trying to install apk programatically from assets folder, But Parse Error I have Googled for days and fine almost everything is fine but still error exists Here is my code
AndroidManifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<application>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths" />
</provider>
</application
file_provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<cache-path
name="cache"
path="." />
<external-cache-path
name="external_cache"
path="." />
<files-path
name="files"
path="." />
<root-path name="root" path="." />
</paths>
MainActivity
private void installApk() {
File f = new File("file:///android_asset/app.apk");
f.setReadable(true, false);
Uri apkUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider",f);
Intent installIntent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
installIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
installIntent.setDataAndType(apkUri, "application/vnd.android.package-archive");
installIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(installIntent);
}
Checking Permission
private void checkWriteExternalStoragePermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (!getPackageManager().canRequestPackageInstalls()) {
startActivity(new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES).setData(Uri.parse(String.format("package:%s", getPackageName()))));
}
}
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
installApk();
} else {
installApk();
}
}
private void requestWriteExternalStoragePermission() {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
} else{
ActivityCompat.requestPermissions(MainActivity.this,new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode==MY_PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE && grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
installApk();
} else {
Toast.makeText(MainActivity.this, "Permission Not Granted.", Toast.LENGTH_SHORT).show();
}
}
File app.apk location : main->assets
Please Note even I write file as
File f = new File(getFilesDir(), "app.apk");
and still parse error
TargetSDK = 31
Upvotes: 1
Views: 425
Reputation: 812
You cannot install APK directly from assets folder because that folder is not accessible to system's package installer. You must first copy that APK to a shared folder like public documents directory and then pass the URI of file from that directory to intent
val publicDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)
Edit: Steps to copy file to external storage:
val file = new File("file:///android_asset/app.apk");
val publicDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)
val destinationAPKFile = File(publicDir, "your_apk_name.apk")
try {
FileUtils.copyFile(file, destinationAPKFile)
}
catch (e: Exception)
{
e.printStackTrace();
}
Then you can use the URI of destinationAPKFile and install APK as you are doing.
Edit 2: In java
File file = new File("file:///android_asset/app.apk");
File publicDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
File destinationAPKFile = File(publicDir, "your_apk_name.apk");
try {
FileUtils.copyFile(file, destinationAPKFile)
}
catch (Exception e)
{
e.printStackTrace();
}
Edit 3: To use FileUtils.copyTo, add following dependency to your app level gradle
implementation group: 'commons-io', name: 'commons-io', version: '2.6'
Upvotes: 1