Question

how should I call columns in a Cursor which I'm returning from custom ContentProvider to allow other apps to read data from it.

I have all columns declared in ProviderContract, but 3rd apps dont know that. Is this documented?

What I want to do in particular is to let the user to share data (file - images and pdfs) from my app to other apps.. Because of security, all data is encrypted on external sd storage. So I need to proxy them through my app to decrypt them. Android security system should handle the issue of only letting other apps read from the content provider after the user choose to share the file. Thats ok, but I have problems that other apps dont know how to read the data from my cursor.

I just feel that Android will have some mechanism to do that:) Either by standardized column names, or by some adapter mechanism.

this is code of my query method..

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    MatrixCursor c = new MatrixCursor(SecuredFileContract.Files.PROJECTION_ALL);
    String name = uri.getLastPathSegment();
    File file = new File(name);
    byte[] fileContents = mStorageProxy.getFileContents(file);
    c.addRow(new Object[]{1, uri.getLastPathSegment(), fileContents, 0});
    return c;
}
Was it helpful?

Solution

a standard for letting the 3rd party app know how to get them from my Contract class..

That's called "a Web page", or some other form of documentation.

For example, the reason you know the column names for the SDK-supplied providers is because that's (somewhat) documented in the JavaDocs.

You need to document:

  • the Uri pattern(s) that you are expecting, particularly for key Uri values (e.g., CONTENT_URI values for queries)

  • the column names that can be requested in a projection

  • the "rules of the game" for how you are interpreting the selection, selectionArgs, and sortOrder parameters to query (e.g., can these be considered ordinary SQL using your column names?)

  • etc.

Whether you supply this documentation in the form of Web pages, a PDF file, a training video, or something else, is up to you.


UPDATE based upon edit:

What I want to do in particular is to let the user to share data (file - images and pdfs) from my app to other apps

Then you need a ContentProvider that supports openFile() or openAssetFile(). Ideally, it also supports query(), but specifically for the OpenableColumns projection, not arbitrary stuff.

This sample project demonstrates what your ContentProvider would need to do, though you would need to replace my read-the-file-from-assets logic with your read-the-encrypted-file-and-decrypt-it logic.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top