Created
September 7, 2025 10:51
-
-
Save root-ansh/1fd4b1a7e3f863d726be24eca463da2a to your computer and use it in GitHub Desktop.
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.openSystemViewerForSAFUri(uri: Uri?) { | |
| uri?:return | |
| val mimeType1 = FileInfo.fromSAFUri(this,uri)?.mimeType | |
| val extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString()) | |
| val mimeType2 = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.lowercase()) | |
| val mimeType3 = "*/*" | |
| val intent = Intent(Intent.ACTION_VIEW).apply { | |
| setDataAndType(uri, mimeType1?:mimeType2?:mimeType3) | |
| addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) | |
| } | |
| try { | |
| startActivity(intent) | |
| } catch (e: ActivityNotFoundException) { | |
| Toast.makeText(this, "No app found to open this file type", Toast.LENGTH_SHORT).show() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment