Last active
December 29, 2019 18:46
-
-
Save farshadrezaee/5b0597bd6d56a7e88bf5d1d9cca4141c to your computer and use it in GitHub Desktop.
install APK from cache directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- add this permission --> | |
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" /> | |
<!-- add provider --> | |
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<paths> | |
<files-path | |
name="files" | |
path="." /> | |
<cache-path | |
name="files" | |
path="." /> | |
</paths> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun Context.requestPackageInstall(file: File?) { | |
if (file == null) { | |
return | |
} | |
val uri: Uri | |
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { | |
uri = FileProvider.getUriForFile(this, "$packageName.provider", file) | |
} else { | |
val fileName = "temp.apk" | |
val fos = openFileOutput(fileName, MODE_WORLD_READABLE or MODE_WORLD_WRITEABLE) | |
val tempFile = File(filesDir, fileName) | |
uri = Uri.fromFile(tempFile) | |
try { | |
fos.write(file.readBytes()) | |
} catch (e: Exception) { | |
Log.e("File_Extension", e.message) | |
} finally { | |
fos.close() | |
} | |
} | |
val intent = Intent(Intent.ACTION_INSTALL_PACKAGE).apply { | |
setDataAndType(uri, "application/vnd.android.package-archive") | |
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) | |
} | |
startActivity(intent) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment