public class

ContentResolverDataProvider

extends InputStreamDataProvider
implements Parcelable UriDataProvider WritableDataProvider
java.lang.Object
   ↳ com.pspdfkit.document.providers.ContextDataProvider
     ↳ com.pspdfkit.document.providers.InputStreamDataProvider
       ↳ com.pspdfkit.document.providers.ContentResolverDataProvider

Class Overview

Data provider for opening PDF documents directly from a ContentProvider using Android's content resolver framework. PSPDFKit internally uses this data provider when loading a document from an Uri that uses the content:// or file:// scheme.

Summary

[Expand]
Inherited Constants
From interface android.os.Parcelable
From interface com.pspdfkit.document.providers.DataProvider
Fields
public static final Creator<ContentResolverDataProvider> CREATOR A field that generates the instances of this class from the provided Parcel.
[Expand]
Inherited Fields
From interface com.pspdfkit.document.providers.DataProvider
Public Constructors
ContentResolverDataProvider(Uri uri)
Creates the data provider for loading a document from the given content provider uri.
Protected Constructors
ContentResolverDataProvider(Parcel in)
Public Methods
boolean canWrite()
Indicates whether this data provider is writable.
int describeContents()
boolean finishWrite()
Called by PSPDFKit after all data has been written via write(byte[]) method.
long getSize()
Return the actual file size of the PDF document which is provided by this data provider.
String getTitle()
Displayable document title to be used if PDF document does not contain metadata.
String getUid()
Unique document identifier used in all caching processes in PSPDFKit.
Uri getUri()
Returns the Uri from which this provided was created.
boolean startWrite(WritableDataProvider.WriteMode writeMode)
Called before writing to the file begins so the provider can prepare file output.
boolean supportsAppending()
Reports whether this provider supports appending to output file as opposed to always rewriting it.
boolean write(byte[] data)
Called by PSPDFKit to write data to the document.
void writeToParcel(Parcel dest, int flags)
Protected Methods
InputStream openInputStream()
Subclasses need to override this method and return a readable input stream which servers the PDF document.
[Expand]
Inherited Methods
From class com.pspdfkit.document.providers.InputStreamDataProvider
From class com.pspdfkit.document.providers.ContextDataProvider
From class java.lang.Object
From interface android.os.Parcelable
From interface com.pspdfkit.document.providers.DataProvider
From interface com.pspdfkit.document.providers.UriDataProvider
From interface com.pspdfkit.document.providers.WritableDataProvider

Fields

public static final Creator<ContentResolverDataProvider> CREATOR

A field that generates the instances of this class from the provided Parcel.

Public Constructors

public ContentResolverDataProvider (Uri uri)

Creates the data provider for loading a document from the given content provider uri.

Parameters
uri A content provider URI with supported scheme (content:// or file://).

Protected Constructors

protected ContentResolverDataProvider (Parcel in)

Public Methods

public boolean canWrite ()

Indicates whether this data provider is writable.

Returns
  • true if the data provider can be written to, false if not.

public int describeContents ()

public boolean finishWrite ()

Called by PSPDFKit after all data has been written via write(byte[]) method. This allows the data provider to close all opened input streams. In case of REWRITE_FILE access, the data provider should here replace the original file with the written temporary file.

Returns
  • true if operation succeeded, false if it failed.

public long getSize ()

Return the actual file size of the PDF document which is provided by this data provider. If the data provider can't determine the correct file size (e.g. in case of an error) this method has to return FILE_SIZE_UNKNOWN.

Returns
  • Returns size of PDF document data in bytes, or FILE_SIZE_UNKNOWN in case of an error.

public String getTitle ()

Displayable document title to be used if PDF document does not contain metadata. For example, for file-based providers this should return the filename. `.pdf` extensions will be stripped automatically.

Returns
  • Fallback document title if PDF document does not contain the required metadata. May be null.

public String getUid ()

Unique document identifier used in all caching processes in PSPDFKit. Must be equal or shorter than 50 chars. This method must be implemented for caching to work properly.

Returns
  • Unique identifier of the provided document. Implementations may not return null or an exception will be thrown.

public Uri getUri ()

Returns the Uri from which this provided was created.

Returns
  • The Uri this data provider was created from.

public boolean startWrite (WritableDataProvider.WriteMode writeMode)

Called before writing to the file begins so the provider can prepare file output.

Parameters
writeMode Determines how file data should be written to the output. For WriteMode#APPEND_TO_FILE the data should be added to the end of existing file data. For WriteMode#REWRITE_FILE PSPDFKit will write full PDF document and the data should be written to a temporary file. The temporary file should replace then original file in finishWrite() callback method. Original file must not be written to when rewriting is in progress - PSPDFKit will issue read(long, long) calls while writing and attempting to write output to the same input will result in a corrupted file.
Returns
  • true if operation succeeded, false if writing should be aborted.

public boolean supportsAppending ()

Reports whether this provider supports appending to output file as opposed to always rewriting it. If this returns false, only REWRITE_FILE will be used and the PDF file will always be fully rewritten on save.

Note that if true is returned here, both WritableDataProvider.WriteMode options must be supported by this DataProvider since some documents cannot be saved incrementally.

Returns

public boolean write (byte[] data)

Called by PSPDFKit to write data to the document. This callback will be called multiple times in a writing operation.

Parameters
data Data to be written to the file.
Returns
  • true if writing succeeded, false if the write operation should be aborted.

public void writeToParcel (Parcel dest, int flags)

Protected Methods

protected InputStream openInputStream ()

Subclasses need to override this method and return a readable input stream which servers the PDF document. This method must return a new InputStream instance every time it is called. This is necessary so InputStreamDataProvider can actually "simulate" random data access on the underlying stream.

Returns
  • A new instance of the InputStream that servers the PDF document.
Throws
IOException