Skip to content

Instantly share code, notes, and snippets.

@Apsaliya
Created June 1, 2020 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Apsaliya/7ff21c3831c2dd35e2b5a1fee36b8ecc to your computer and use it in GitHub Desktop.
Save Apsaliya/7ff21c3831c2dd35e2b5a1fee36b8ecc to your computer and use it in GitHub Desktop.
On Activity result restore
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