Created
June 1, 2020 16:13
-
-
Save Apsaliya/7ff21c3831c2dd35e2b5a1fee36b8ecc to your computer and use it in GitHub Desktop.
On Activity result restore
This file contains 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
override fun onActivityResult( | |
requestCode: Int, | |
resultCode: Int, | |
resultIntent: Intent? | |
) { | |
lifecycleScope.launch { | |
if (resultCode == Activity.RESULT_OK && resultIntent != null) { | |
val uri = resultIntent.data!! | |
contentResolver | |
.takePersistableUriPermission( | |
uri, | |
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION | |
) | |
val pfd = contentResolver.openFileDescriptor(uri, "r") | |
pfd?.use { | |
FileInputStream(pfd.fileDescriptor).use { inputStream -> | |
try { | |
val restoreResult = restoreFromInputStream( | |
context, | |
inputStream | |
) | |
if (restoreResult) { | |
// Success action | |
} else { | |
//failure action | |
} | |
} catch (e: IncorrectPasswordException) { | |
// incorrect password | |
} | |
} | |
} | |
} | |
} | |
super.onActivityResult(requestCode, resultCode, resultIntent) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment