Created
July 12, 2022 04:52
-
-
Save uc-sja/717b82564f49def54f51bbded91ba719 to your computer and use it in GitHub Desktop.
filerequesthandler
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
/* | |
* 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