...
We use the term file import
to refer to the Share Open with feature within Gini Bank SDK. From the point of view of the SDK, files are imported into the SDK from an outside source. It is unaware and can't set configuration related to enabling the client app to receive files via Android’s Open-with/Share functionality.
To enable this feature, pass true
to GiniBank.setCaptureConfiguration(CaptureConfiguration(fileImportEnabled = ))
. Additionally, your app should declare intent filters for receiving images and/or PDF files from other apps and then forward the incoming intent to Gini Bank SDK.
...
Register image and PDF file types
Add the following intent filter to the activity in your AndroidManifest.xml
to receive incoming images and PDF files:
...
We recommend adding ACTION_VIEW to the intent filter to let users send images and PDF files to your app from apps that don’t implement sharing with ACTION_SEND but enable viewing images and PDF files with other apps.
Handle imported files
When your app is requested to handle an image or a PDF file, your activity (declaring the intent filter shown above) is launched or resumed (onNewIntent()
) with the Intent
having ACTION_VIEW
or ACTION_SEND
.
...
Code Block | ||
---|---|---|
| ||
fun configureCapture() {
GiniBank.setCaptureConfiguration(this,
CaptureConfiguration(
fileImportEnabled = true,
... // other configuration options
)
)
}
// Use the androidx's Activity Result API to register a handler for the capture result.
val captureImportLauncher = registerForActivityResult(CaptureFlowImportContract()) { result: CaptureResult ->
when (result) {
is CaptureResult.Success -> {
handleExtractions(result.specificExtractions)
}
is CaptureResult.Error -> {
when (result.value) {
is ResultError.Capture -> {
val captureError: GiniCaptureError = (result.value as ResultError.Capture).giniCaptureError
handleCaptureError(captureError)
}
is ResultError.FileImport -> {
val fileImportError = result.value as ResultError.FileImport
handleFileImportError(fileImportError)
}
}
}
CaptureResult.Empty -> {
handleNoExtractions()
}
CaptureResult.Cancel -> {
handleCancellation()
}
}
}
fun handleFileImportError(exception: ImportedFileValidationException) {
var message = ...
exception.validationError?.let { validationError ->
// Get the default message
message = getString(validationError.textResource)
// Or use custom messages
message = when (validationError) {
FileImportValidator.Error.TYPE_NOT_SUPPORTED -> ...
FileImportValidator.Error.SIZE_TOO_LARGE -> ...
FileImportValidator.Error.TOO_MANY_PDF_PAGES -> ...
FileImportValidator.Error.PASSWORD_PROTECTED_PDF -> ...
FileImportValidator.Error.TOO_MANY_DOCUMENT_PAGES -> ...
}
}
AlertDialog.Builder(this)
.setMessage(message)
.setPositiveButton("OK") { _, _ -> finish() }
.show()
}
fun startGiniBankSDKForImportedFile(importedFileIntent: Intent) {
// Configure capture first
configureCapture();
fileImportCancellationToken =
GiniBank.startCaptureFlowForIntent(captureImportLauncher, this, importedFileIntent)
} |
Launch the SDK from a different activity
When your activity (declaring the intent filter shown above) is not the same as the activity from which you launch the Gini Bank SDK you need to create a Document
from the intent first and then pass it to your other activity as an extra. There you can then launch the SDK with the Document
.
Note |
---|
Passing the launch intent from the first activity to the other activity causes |
Create a
Document
from the intent in your activity A:Code Block language kotlin fun createDocumentAndLaunchActivityB(importedFilesIntent: Intent) { // Configure capture first configureCapture(); fileImportCancellationToken = GiniBank.createDocumentForImportedFiles( intent = intent, context = this, callback = { documentCreationResult -> when (documentCreationResult) { GiniBank.CreateDocumentFromImportedFileResult.Cancelled -> showCancellationToast("'Open with' cancelled") is GiniBank.CreateDocumentFromImportedFileResult.Error -> showErrorToast("'Open with' failed with error ${documentCreationResult.error}") is GiniBank.CreateDocumentFromImportedFileResult.Success -> documentCreationResult.document?.let { document -> launchActivityB(document) } ?: run { showErrorToast("'Open with' failed") } } } ) }
Get the
Document
from the intent extras in your activity B and launch the Gini Bank SDK:Code Block language kotlin override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) if (savedInstanceState == null) { if (intent.hasExtra(EXTRA_IN_OPEN_WITH_DOCUMENT)) { IntentCompat.getParcelableExtra(intent, EXTRA_IN_OPEN_WITH_DOCUMENT, Document::class.java)?.let { GiniBank.startCaptureFlowForDocument( resultLauncher = captureImportLauncher, document = document ) } } } }
Detect when your app is the default app for opening images or PDF files
Android users can unintentionally set your app as the default for opening images or PDF files. To inform users about it and let them undo it, the SDK detects if your app is selected as the default app and shows the Clear Defaults Dialog enabling them to clear the defaults for your app in the Android settings.
...