Question

I'm trying to programmatically start the contacts app to import a big .vcf file with a lot of contacts. The following code works almost perfect.

String vcfMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("vcf");
Intent openVcfIntent = new Intent(Intent.ACTION_VIEW);
openVcfIntent.setDataAndType(Uri.fromFile(savedVCardFile), vcfMimeType);
startActivity(openVcfIntent);

The only problem is that Android shows an app chooser dialog, showing not only the contacts app, but also Dropbox (or any other app that handles vCard files). I want to prevent this behavior and directly open the file with the contacts app, so that the import starts automatically.

I've tried several things found on StackOverflow with no luck, such as setting:

openVcfIntent.setComponent(new ComponentName("com.android.contacts", "com.android.contacts.DialtactsContactsEntryActivity"));
openVcfIntent.setAction("android.intent.action.MAIN");                                                                       
openVcfIntent.addCategory("android.intent.category.LAUNCHER");                                                               
openVcfIntent.addCategory("android.intent.category.DEFAULT");                                                                

Any ideas on how to approach the issue?

Was it helpful?

Solution

Answering my own question. I finally found a way to automatically open a vCard with the contacts app, thanks to this answer about explicit intents.

private void openBackup(File savedVCard)
{
    try
    {
        String vcfMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("vcf");
        Intent openVcfIntent = new Intent(Intent.ACTION_VIEW);
        openVcfIntent.setDataAndType(Uri.fromFile(savedVCard), vcfMimeType);
        // Try to explicitly specify activity in charge of opening the vCard so that the user doesn't have to choose
        // https://stackoverflow.com/questions/6827407/how-to-customize-share-intent-in-android/9229654#9229654
        try
        {
            if (getActivity().getPackageManager() != null)
            {
                List<ResolveInfo> resolveInfos = getActivity().getPackageManager().queryIntentActivities(openVcfIntent, 0);
                if (resolveInfos != null)
                {
                    for (ResolveInfo resolveInfo : resolveInfos)
                    {
                        ActivityInfo activityInfo = resolveInfo.activityInfo;
                        if (activityInfo != null)
                        {
                            String packageName = activityInfo.packageName;
                            String name = activityInfo.name;
                            // Find the needed Activity based on Android source files: http://grepcode.com/search?query=ImportVCardActivity&start=0&entity=type&n=
                            if (packageName != null && packageName.equals("com.android.contacts") && name != null && name.contains("ImportVCardActivity"))
                            {
                                openVcfIntent.setPackage(packageName);
                                break;
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ignored)
        {
        }

        startActivity(openVcfIntent);
    }
    catch (Exception exception)
    {
        // No app for openning .vcf files installed (unlikely)
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top