Skip to content

Instantly share code, notes, and snippets.

@uc-sja
Created July 12, 2022 04:52
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 uc-sja/717b82564f49def54f51bbded91ba719 to your computer and use it in GitHub Desktop.
Save uc-sja/717b82564f49def54f51bbded91ba719 to your computer and use it in GitHub Desktop.
filerequesthandler
/*
* When the Activity of the app that hosts files sets a result and calls
* finish(), this method is invoked. The returned Intent contains the
* content URI of a selected file. The result code indicates if the
* selection worked or not.
*/
public override fun onActivityResult(requestCode: Int, resultCode: Int, returnIntent: Intent) {
// If the selection didn't work
if (resultCode != Activity.RESULT_OK) {
// Exit without doing anything else
return
}
// Get the file's content URI from the incoming Intent
returnIntent.data?.also { returnUri ->
/*
* Try to open the file for "read" access using the
* returned URI. If the file isn't found, write to the
* error log and return.
*/
inputPFD = try {
/*
* Get the content resolver instance for this context, and use it
* to get a ParcelFileDescriptor for the file.
*/
contentResolver.openFileDescriptor(returnUri, "r")
} catch (e: FileNotFoundException) {
e.printStackTrace()
Log.e("MainActivity", "File not found.")
return
}
// Get a regular file descriptor for the file
val fd = inputPFD.fileDescriptor
...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment