gunescelil
gunescelil

Reputation: 313

Android resolveContentProvider returns null

I want to do the following in my application. First I need to login. Then, after log in I quit my application. But it is running at the background. Log in records still hold. Then I need to choose a file from a file browser. After choosing the file I press the share option. Then I choose my application to do some operations on the file. After I make the operations on the file I record it to the cache. Then I want to open this file on some other appropriate application. I can do all of them but there is a problem while calling getUriForFile method.

String mimeType = getMimeType(fileUri);//returns mime type

File imagePath = new File(context.getCacheDir(), "/");
String fileUriStr = fileUri.getLastPathSegment();
File newFile = new File(imagePath, fileUriStr);
try {
     newFile.createNewFile();
} catch (IOException e) {
            e.printStackTrace();
}    
uri = FileProvider.getUriForFile(context, context.getPackageName(), newFile);

In Manifest I added a fileProvider.

<activity
    android:name=".MainActivity"
    android:label="@string/app_name">

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.share"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>

    <!--Normal way opener -->
    <intent-filter android:label="appName">
        <action android:name="android.intent.action.MAIN" />
    </intent-filter>

    <!--From share options opener -->
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <action android:name="android.intent.action.SEND_MULTIPLE" />

        <category android:name="android.intent.category.DEFAULT" />

        <data android:mimeType="*/*" />
    </intent-filter>
</activity>

In file_paths.xml I have following code:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path name="cache_docs" path="data/data/com.app.firm.app/cache" />

</paths>

I opened FileProvider.java file. I get the error from:

final ProviderInfo info = context.getPackageManager()
                .resolveContentProvider(authority, PackageManager.GET_META_DATA);
        final XmlResourceParser in = info.loadXmlMetaData(
                context.getPackageManager(), META_DATA_FILE_PROVIDER_PATHS);
        if (in == null) {
            throw new IllegalArgumentException(
                    "Missing " + META_DATA_FILE_PROVIDER_PATHS + " meta-data");
        }

info parameter is null. So when the program calls info.loadXmlMetaData it gets null pointer exception. So context.getPackageManager returns null. Remember that at that time I have two applications running at the same time. One of follows natural flow (i.e -> Login and main activity). The other just opens from MainActivity. The context.getPackageManager is called from the second running application. Please help me

Upvotes: 4

Views: 2535

Answers (1)

wanbo
wanbo

Reputation: 958

I know how to solve this problem , I think I can help you.

Maybe you have two <provider> , one of in Lib, one of your project

You can custom the class extends FileProvider, and change the <provide> name tag to your custom FileProvider:

<provider
    android:name="XXXX.YourCustomFileProvider"
    android:authorities="${applicationId}.share"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

And the problem will be solved.

Thanks!

Upvotes: 3

Related Questions