Skip to content Skip to sidebar Skip to footer

How Do I Open A .vcf From An Intent Filer?

Hello I'm trying to make an app where I can open .vcf files. Primarily from texting apps and I've gotten it to work where I can open my app using the following code in and androidm

Solution 1:

To get the Uri representing the content you are to view or edit, call getIntent().getData(). Then, you can use ContentResolver and openInputStream() to get an InputStream on the actual content you are offering to view or edit, given that Uri.

In terms of vCard itself, Android has nothing built-in for vCard parsing, in terms of a traditional Java API. Your choices are:

  • Use this third-party library

  • Find another third-party library or other vCard parsing hunk of code (e.g., the Contacts app should have some in there somewhere)

  • Roll your own parser sufficient for your needs

Solution 2:

if (Build.VERSION.SDK_INT<=Build.VERSION_CODES.KITKAT) { 
     mIntent.setAction(Intent.ACTION_GET_CONTENT);
     mIntent.setType("text/x-vcard");
     startActivityForResult(mIntent, 1);
} else {
     mIntent.setAction(Intent.ACTION_OPEN_DOCUMENT);
     mIntent.addCategory(Intent.CATEGORY_OPENABLE);
     mIntent.setDataAndType(Uri.fromFile(contactFolder), "text/x-vcard");
     startActivityForResult(Intent.createChooser(mIntent, "Select File"), 2);
}

And get intent result.....

@SuppressLint("NewApi")
@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        Uri mUri = data.getData();
        if (mUri != null) {
            String mFilePath;
            if (requestCode == 1) {
                mFilePath = mUri.getPath();
            } else {
                mFilePath = getPathFromURI(mUri);
            }
            Intent mIntent = newIntent(this, RestoreActivity.class);
            mIntent.putExtra("filepath", mFilePath);
            startActivity(mIntent);
        } else {
            Toast.makeText(this, "Something went wrong...\nPlease try again...", Toast.LENGTH_SHORT).show();
        }
    }
}

@SuppressLint("NewApi")
privateStringgetPathFromURI(Uri mUri) {
    String mDocId = DocumentsContract.getDocumentId(mUri);
    String[] mSplit = mDocId.split(":");
    returnEnvironment.getExternalStorageDirectory() + File.separator + mSplit[1];
}

Solution 3:

It's actually not a very complicated process. Just ensure that you get the Uri from FileProvider and you'll be able to trigger the Phone Book Chooser Intent.

Then, you'll be able to import your contacts with .vcf.

privatefunsaveVcfFile(savedVCard: File) {
    try {
        val intent = Intent(Intent.ACTION_VIEW)
        val uri = FileProvider.getUriForFile(
            this,
            "${BuildConfig.APPLICATION_ID}.fileprovider",
            savedVCard
        )
        intent.setDataAndType(uri, contentResolver.getType(uri))
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        startActivity(intent)
    } catch (exception: Exception) {
        // TODO: Handle Exception
    }
}

Post a Comment for "How Do I Open A .vcf From An Intent Filer?"