ContentResolver.openFileDescriptor() does not call ContentProvider.openFIle

android, android-contentprovider, storage-access-framework

Solution

That seems like a bug in openFileDescriptor() to me. I ended up calling `ContentResolver.openInputStream()/ContentResolver.openOutputStream()`. These methods correclty call `openFile()` to get to the file.

Alternatively you can do something like this:

ContentResolver resolver = context.getContentResolver();
ContentProviderClient providerClient = resolver.acquireContentProviderClient(uri);
ParcelFileDescriptor descriptor = providerClient.openFile(uri, "r");

Dont forget to release ContentProviderClient.

Problem

I implemented a `ContentProvider` by overriding `openFile(...)` method. Then I tested it by using `ContentResolver.openFileDescriptor(...)` method. As specified in the javadoc, `openFileDescriptor` suppose to call `ContentProvider.openFile`: "...Open a raw file descriptor to access data under a URI. This is like `openAssetFileDescriptor(Uri, String)`, but uses the underlying `openFile(Uri, String)` `ContentProvider.openFile()` method..." The problem is the `openFile()` method was never called. Instead I observed that `openAssetFile()` was called instead. I tested it with android 4.1.2r1. When I review the source code for the android class ContentResolver (4.1.2) I see that `openFileDescriptor()` will just call `openAssetFileDescriptor()`, and I cannot find any execution path where `ContentProvider.openFile()` would be called at all. Anybody has any idea where I was wrong? - Thanks Here is my test method call: ``` context.getContentResolver().openFileDescriptor(bitmapUri, "r").getFileDescriptor(); ``` My `bitmapUri` is something like this "content://com.myprovider.authority/filename"

Original source