---
title: "Custom Data Provider"
canonical_url: "https://www.nutrient.io/guides/android/samples/custom-data-provider-kotlin/"
md_url: "https://www.nutrient.io/guides/android/samples/custom-data-provider-kotlin.md"
last_updated: "2026-05-25T09:22:39.135Z"
description: "Implement a custom DataProvider to load a PDF from app resources."
---

# Custom Data Provider

Implement a custom DataProvider to load a PDF from app resources.

[Get Started](https://www.nutrient.io/sdk/android/getting-started.md)

[All Samples](https://www.nutrient.io/guides/android/samples.md)

[Download](https://www.nutrient.io/guides/android/downloads.md)

[Launch Demo](https://www.nutrient.io/demo/)

---

```kotlin

/*
 *   Copyright © 2020-2026 PSPDFKit GmbH. All rights reserved.
 *
 *   The PSPDFKit Sample applications are licensed with a modified BSD license.
 *   Please see License for details. This notice may not be removed from this file.
 */

package com.pspdfkit.catalog.examples.kotlin

import android.content.Context
import android.content.res.Resources
import android.os.Parcel
import android.os.Parcelable
import androidx.annotation.RawRes
import com.pspdfkit.catalog.R
import com.pspdfkit.catalog.SdkExample
import com.pspdfkit.configuration.activity.PdfActivityConfiguration
import com.pspdfkit.document.providers.DataProvider
import com.pspdfkit.document.providers.InputStreamDataProvider
import com.pspdfkit.ui.PdfActivity
import com.pspdfkit.ui.PdfActivityIntentBuilder
import java.io.IOException
import java.io.InputStream

/**
 * This example shows how to create a custom data provider that reads a document from the `raw` resources
 * of the app. Furthermore, it implements [Parcelable] to allow using the data provider with  [PdfActivity].
 */
class CustomDataProviderExample(context: Context) :
    SdkExample(context, R.string.customDataProviderExampleTitle, R.string.customDataProviderExampleDescription) {
    override fun launchExample(context: Context, configuration: PdfActivityConfiguration.Builder) {
        // Create an instance of the custom data provider. See the implementation details below.
        val dataProvider: DataProvider = RawResourceDataProvider(R.raw.guide)

        // Start the activity using our custom data provider.
        val intent =
            PdfActivityIntentBuilder.fromDataProvider(context, dataProvider).configuration(configuration.build()).build()
        context.startActivity(intent)
    }
}

/**
 * Custom data provider for loading a PDF document from the app's raw resources. Since
 * [Resources.openRawResource] returns an `InputStream`, this provider derives from
 * `InputStreamDataProvider` which handles loading data from a stream object.
 *
 * @param resId The id of the PDF document inside the resources (stored within the `res/raw` folder of the application).
 */
class RawResourceDataProvider(@param:RawRes private val resId: Int) :
    InputStreamDataProvider(),
    Parcelable {
    /**
     * The size of the raw resource. This will be cached after the first call to [.getSize].
     */
    private var size = DataProvider.FILE_SIZE_UNKNOWN.toLong()

    /**
     * We return the InputStream for the referenced raw resource. Since InputStreamDataProvider may call this
     * method multiple times we have to make sure that it always returns a fresh input stream object.
     */
    @Throws(IOException::class)
    override fun openInputStream(): InputStream = getContext().resources.openRawResource(resId)

    /**
     * This method returns the size of our resource. Android only gives us an [InputStream] for
     * accessing the resources. Thus we have to reopen the input stream if current stream
     * position is not at the start.
     */
    override fun getSize(): Long {
        // If the file size is already known, return it immediately.
        if (size!= DataProvider.FILE_SIZE_UNKNOWN.toLong()) {
            return size
        }

        val inputStreamSize =
            try {
                // Since we can only get size of the available data in the input stream we need to
                // reopen it here if the stream position is not 0.
                if (inputStreamPosition!= 0L) {
                    reopenInputStream()
                }
                openInputStream().available()
            } catch (e: Exception) {
                DataProvider.FILE_SIZE_UNKNOWN
            }

        size = inputStreamSize.toLong()
        return size
    }

    override fun getUid(): String = getContext().resources.getResourceName(resId)

    override fun getTitle(): String {
        // If you know the file or document name upfront, you can return it here. Otherwise return null,
        // which will instruct Nutrient to use the title stored within the document (if any).
        return "PSPDFKit Quickstart Guide"
    }

    // The code below is standard Android parcelation code. If you don't know how to implement the Parcelable
    // interface start looking at {@link http://developer.android.com/reference/android/os/Parcelable.html}.

    /**
     * Default parcelable implementation. The object is always parceled the same way. Thus, we return 0.
     */
    override fun describeContents(): Int = 0

    /**
     * We simply write the id of the PDF resource to the parcel.
     */
    override fun writeToParcel(dest: Parcel, flags: Int) {
        dest.writeInt(resId)
    }

    /**
     * Constructor required for unparcelation, takes a `input` Parcel and reads the raw resource id from it.
     */
    internal constructor(input: Parcel) : this(input.readInt())

    companion object CREATOR : Parcelable.Creator<RawResourceDataProvider> {
        override fun createFromParcel(parcel: Parcel): RawResourceDataProvider = RawResourceDataProvider(parcel)

        override fun newArray(size: Int): Array<RawResourceDataProvider?> = arrayOfNulls(size)
    }
}

```

This code sample is an example that illustrates how to use our SDK. Please adapt it to your specific use case.

---

## Related pages

- [JavaScript Form Filling](/guides/android/samples/javascript-form-filling-kotlin.md)
- [Merge Documents](/guides/android/samples/merge-documents-kotlin.md)
- [Overlay Visibility](/guides/android/samples/overlay-visibility-kotlin.md)
- [Password Protected PDF](/guides/android/samples/password-protected-pdf-kotlin.md)
- [Reader View](/guides/android/samples/reader-view-kotlin.md)
- [PdfFragment](/guides/android/samples/pdffragment-kotlin.md)
- [Scientific Paper](/guides/android/samples/scientific-paper-kotlin.md)
- [Signature Storage Database](/guides/android/samples/signature-storage-database-kotlin.md)
- [Playground](/guides/android/samples/playground-kotlin.md)
- [Try Instant](/guides/android/samples/try-instant-kotlin.md)
- [Selection Customization](/guides/android/samples/selection-customization-java.md)
- [Text Field Suggestions](/guides/android/samples/text-field-suggestions-kotlin.md)
- [Annotation Rendering](/guides/android/samples/annotation-rendering-kotlin.md)
- [Custom Form Highlight Color](/guides/android/samples/custom-form-highlight-color-java.md)
- [Application Policy](/guides/android/samples/application-policy-kotlin.md)
- [Compose Image Document](/guides/android/samples/compose-image-document-kotlin.md)
- [Digital Signature (Basic)](/guides/android/samples/digital-signature-basic-kotlin.md)
- [Disabled Annotation Property](/guides/android/samples/disabled-annotation-property-java.md)
- [LTV Signature](/guides/android/samples/ltv-signature-kotlin.md)
- [Popup Toolbar Customization](/guides/android/samples/popup-toolbar-customization-kotlin.md)
- [PdfUiFragment](/guides/android/samples/pdfuifragment-kotlin.md)
- [Programmatic Zoom](/guides/android/samples/programmatic-zoom-kotlin.md)
- [OCR](/guides/android/samples/ocr-kotlin.md)
- [PDF from Image](/guides/android/samples/pdf-from-image-kotlin.md)
- [Remote URL](/guides/android/samples/remote-url-kotlin.md)
- [Split View](/guides/android/samples/split-view-java.md)
- [Sound Extraction](/guides/android/samples/sound-extraction-kotlin.md)
- [Tabbed Documents](/guides/android/samples/tabbed-documents-kotlin.md)
- [Signature Parcelize](/guides/android/samples/signature-parcelize-kotlin.md)
- [Vertical Scrollbar](/guides/android/samples/vertical-scrollbar-java.md)
- [Thumbnail Bar Modes](/guides/android/samples/thumbnail-bar-modes-kotlin.md)
- [XFDF Import/Export](/guides/android/samples/xfdf-import-export-kotlin.md)
- [UI View Modes](/guides/android/samples/ui-view-modes-kotlin.md)
- [Runtime Configuration](/guides/android/samples/runtime-configuration-kotlin.md)
- [Watermarks](/guides/android/samples/watermarks-kotlin.md)
- [Form Click Intercept (Compose)](/guides/android/samples/form-click-intercept-compose-kotlin.md)
- [Annotation Configuration](/guides/android/samples/annotation-configuration-kotlin.md)
- [AI Assistant (Multiple Documents, ViewPager)](/guides/android/samples/ai-assistant-multiple-documents-viewpager-kotlin.md)
- [Annotation Flags](/guides/android/samples/annotation-flags-kotlin.md)
- [Custom Activity Form Editing](/guides/android/samples/custom-activity-form-editing-java.md)
- [Compose Navigation](/guides/android/samples/compose-navigation-kotlin.md)
- [Annotation Selection Styling](/guides/android/samples/annotation-selection-styling-kotlin.md)
- [Custom ActionBar Actions](/guides/android/samples/custom-actionbar-actions-kotlin.md)
- [Annotation Overlay](/guides/android/samples/annotation-overlay-java.md)
- [Custom Layout](/guides/android/samples/custom-layout-kotlin.md)
- [Custom Activity Toolbars](/guides/android/samples/custom-activity-toolbars-java.md)
- [Custom Note Hinter](/guides/android/samples/custom-note-hinter-kotlin.md)
- [Custom Sharing Menu](/guides/android/samples/custom-sharing-menu-java.md)
- [Custom Main Toolbar](/guides/android/samples/custom-main-toolbar-kotlin.md)
- [Custom Search UI (Compose)](/guides/android/samples/custom-search-ui-compose-kotlin.md)
- [Annotations with Transparency](/guides/android/samples/annotations-with-transparency-kotlin.md)
- [Digital Signature (Manual)](/guides/android/samples/digital-signature-manual-kotlin.md)
- [Custom Download Dialog](/guides/android/samples/custom-download-dialog-java.md)
- [Custom Sharing Dialog](/guides/android/samples/custom-sharing-dialog-java.md)
- [Custom Outline Provider](/guides/android/samples/custom-outline-provider-kotlin.md)
- [Custom Stamp Annotations](/guides/android/samples/custom-stamp-annotations-java.md)
- [Digital Signature (Two-Step)](/guides/android/samples/digital-signature-two-step-kotlin.md)
- [Document Download](/guides/android/samples/document-download-kotlin.md)
- [Custom Toolbar Grouping](/guides/android/samples/custom-toolbar-grouping-java.md)
- [Document from Canvas](/guides/android/samples/document-from-canvas-kotlin.md)
- [Document Switcher](/guides/android/samples/document-switcher-java.md)
- [Custom Page Templates](/guides/android/samples/custom-page-templates-java.md)
- [Digital Signature (Third-Party)](/guides/android/samples/digital-signature-third-party-kotlin.md)
- [Image Document](/guides/android/samples/image-document-kotlin.md)
- [JavaScript Calculator](/guides/android/samples/javascript-calculator-kotlin.md)
- [Inline Multimedia](/guides/android/samples/inline-multimedia-kotlin.md)
- [DocumentView Composable](/guides/android/samples/documentview-composable-kotlin.md)
- [Kiosk Grid](/guides/android/samples/kiosk-grid-kotlin.md)
- [Multimedia Annotations](/guides/android/samples/multimedia-annotations-kotlin.md)
- [Overlay Views](/guides/android/samples/overlay-views-kotlin.md)
- [Measurement Tools](/guides/android/samples/measurement-tools-kotlin.md)
- [Search Indexing](/guides/android/samples/search-indexing-kotlin.md)
- [AI Assistant (Single Document)](/guides/android/samples/ai-assistant-single-document-kotlin.md)
- [Annotation Sidebar](/guides/android/samples/annotation-sidebar-kotlin.md)
- [Bookmark Highlighting](/guides/android/samples/bookmark-highlighting-kotlin.md)
- [AI Assistant (Multiple Documents, Compose)](/guides/android/samples/ai-assistant-multiple-documents-compose-kotlin.md)
- [Custom Annotation Inspector](/guides/android/samples/custom-annotation-inspector-java.md)
- [Custom Electronic Signature](/guides/android/samples/custom-electronic-signature-java.md)
- [Custom Annotation Creation Toolbar](/guides/android/samples/custom-annotation-creation-toolbar-java.md)
- [Document Comparison](/guides/android/samples/document-comparison-kotlin.md)
- [Document Processing](/guides/android/samples/document-processing-kotlin.md)
- [File Annotation Creation](/guides/android/samples/file-annotation-creation-kotlin.md)
- [Document Sharing](/guides/android/samples/document-sharing-java.md)
- [Dynamic Pages on Scroll](/guides/android/samples/dynamic-pages-on-scroll-kotlin.md)
- [Form Filling](/guides/android/samples/form-filling-kotlin.md)
- [Download Progress](/guides/android/samples/download-progress-kotlin.md)
- [Add LTV to Existing Signature](/guides/android/samples/add-ltv-to-existing-signature-kotlin.md)
- [Fragment Runtime Configuration](/guides/android/samples/fragment-runtime-configuration-kotlin.md)
- [Instant Document JSON](/guides/android/samples/instant-document-json-kotlin.md)
- [JavaScript Actions](/guides/android/samples/javascript-actions-kotlin.md)
- [Instant JSON Attachment](/guides/android/samples/instant-json-attachment-kotlin.md)
- [Inline Search](/guides/android/samples/inline-search-java.md)
- [Form Creation](/guides/android/samples/form-creation-kotlin.md)
- [HTML-to-PDF Conversion](/guides/android/samples/html-to-pdf-conversion-kotlin.md)
- [Tabbed Documents (Persistent)](/guides/android/samples/tabbed-documents-persistent-kotlin.md)
- [Annotation Creation](/guides/android/samples/annotation-creation-kotlin.md)
- [Electronic + Digital Signing](/guides/android/samples/electronic-digital-signing-kotlin.md)
- [External Document](/guides/android/samples/external-document-kotlin.md)
- [E-Learning](/guides/android/samples/e-learning-kotlin.md)
- [Generate PDF Report](/guides/android/samples/generate-pdf-report-kotlin.md)
- [Forms with JavaScript](/guides/android/samples/forms-with-javascript-kotlin.md)
- [Multiple Documents (Compose Pager)](/guides/android/samples/multiple-documents-compose-pager-kotlin.md)
- [AES Encrypted File](/guides/android/samples/aes-encrypted-file-java.md)
- [Construction Floor Plan](/guides/android/samples/construction-floor-plan-kotlin.md)
- [Filterable Thumbnail Grid](/guides/android/samples/filterable-thumbnail-grid-kotlin.md)
- [Screen Reader](/guides/android/samples/screen-reader-java.md)
- [Custom Search UI (Views)](/guides/android/samples/custom-search-ui-views-java.md)
- [Hide and Reveal Areas](/guides/android/samples/hide-and-reveal-areas-kotlin.md)

