← All articlesMobile applications

Importing from ContentProvider: URI, DISPLAY_NAME and a Local Copy

When importing a document from ContentProvider, check both the write destination and how the incoming content is selected. The application has already received a content URI: trace that URI through to the opening of the read descriptor. DISPLAY_NAME, read from a Cursor, remains a label for the interface. If that name is used to select a stream or return a previously saved copy, two documents with the same name may get mixed up. We will examine three separate data flows and test them using a fully fictional provider owned by the team.

Three data flows instead of one document name

Start with the content URI already received. The first flow leads from it to ContentResolver.openFileDescriptor(uri, "r"). The method returns a ParcelFileDescriptor, from which a FileDescriptor is obtained to read the content. The second flow begins with a metadata query: the DISPLAY_NAME value from the Cursor goes to the interface. The third connects the resulting descriptor to the bytes in the application's own local copy.

The trust boundary lies where the application assigns internal roles to external values. The received name can serve as a display label, but in the proposed design it neither selects the input stream nor becomes the key for returning a previously imported document. An internally generated destination identifier is a prerequisite for this design; the main task here is to preserve the connection between the incoming URI, the opened descriptor and the copied bytes.

What DISPLAY_NAME means

In the standard FileProvider implementation, query() returns a Cursor containing the name and size of the file associated with the content URI. DISPLAY_NAME contains a string name matching File.getName(). This describes the standard FileProvider implementation; it should not be assumed to describe how every arbitrary provider works. The name is not defined as a unique content identifier.

The metadata example displays DISPLAY_NAME and SIZE in separate TextView elements. When testing your client, preserve this separation of roles: the name tells the user which document is displayed, while reading starts with the received URI. Trace whether DISPLAY_NAME enters a local-copy lookup whose result allows the application to skip opening the current URI.

Fictional example: two documents named “Notes.txt”

Imagine a fictional application called “Draft” and a synthetic ContentProvider owned by the team. In the test configuration, URI A returns DISPLAY_NAME “Notes.txt” and predefined bytes containing “Document A”. URI B returns the same name, but bytes containing “Document B”. All documents and expected results are created specifically for testing; no real user data is needed.

The error in this example can be described without dangerous names or path substitution: after importing A, the application looks up a saved document by the name “Notes.txt” and returns the copy of A when importing B. The requirement being tested is different: each import must open its own received URI and save the bytes from the corresponding descriptor. Matching interface labels are no reason to treat these two inputs as one document.

Positive control: trace the entire import

The following scenarios are original editorial suggestions for defensive tests, not an official Android procedure. First, configure only URI A, its display name and known synthetic bytes. Start the import from the URI already received and record observations at the boundaries of the three data flows.

Acceptance criteria must confirm the work performed: openFileDescriptor receives URI A and mode r; the DISPLAY_NAME read is shown in the interface; and the application's own local copy matches the expected content byte for byte. In the copying example, the input FileInputStream is created from the previously obtained fd—check this exact connection when reviewing the code. An import success message is insufficient: it does not show where the data was read from or what ended up in the copy.

The main test: matching names, different streams

Now import A and B in sequence with the same DISPLAY_NAME. For each operation, separately check the argument used to open the descriptor and the actual content of the result. The first import should produce the bytes “Document A”, and the second should produce “Document B”. The interface name remains “Notes.txt” in both cases, as configured for the test.

The test fails if the second import returns the copy of A because the names match, even when the interface reports success. To fix this, first remove DISPLAY_NAME from input-stream selection and from this reuse logic. Then verify that the copy operation receives the descriptor opened for the current URI. Repeat the scenario and confirm both results by comparing bytes, rather than merely checking that an entry appears in the document list.

The name changes, the read conditions remain the same

In the second scenario, import URI A, then change only DISPLAY_NAME in your own test provider and import the same URI again. Under the conditions of this test, the provider keeps the bytes unchanged. The expected result is that the interface shows the new name, reading opens through the same received URI A, and the local copy contains the predefined bytes “Document A”.

This result confirms the separation of metadata and reading in the specified configuration. It does not mean that every URI always represents unchanging content or that an arbitrary provider preserves bytes when a name changes. The priority is to observe the connection between the current URI, the descriptor and the import result; matching URIs or a changed label alone cannot replace comparison with the expected test data.

Results and the limits of testing

After the fixes, the team should have three verifiable results: the URI controls opening for reading, DISPLAY_NAME goes to the interface, and the bytes from the resulting descriptor reach the application's own local copy. The matching-name and renaming scenarios supplement the positive control. Passing them confirms specific behavior on prepared data, but provides no general or statistical guarantee of import security.

This article is limited to separating the incoming URI, metadata and content copying. It does not cover outgoing FileProvider configuration, permissions, archives, server uploads, format validation, limits or write atomicity. If your product has a website or web component, CheckMyApp offers a free website or web-product audit with a separate review scope.

Sources and documentation

  1. Improperly trusting ContentProvider-provided filename
  2. Retrieving file information
  3. Requesting a shared file
CheckMyApp

Check your application’s security.

Enter your web application address and email. Get a free preliminary report and choose the depth of analysis.

Check for free