How do I open a .vcf from an intent filer?

安稳与你 提交于 2020-12-16 05:40:45

问题


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 androidmanifest.xml

 <activity android:name=".Home"
       android:parentActivityName=".MapsActivity">

       <meta-data android:name="android.support.PARENT_ACTIVITY"
        android:value="MapsActivity" />
           <intent-filter android:label="Open iLocation">
              <action android:name="android.intent.action.VIEW" />
               <action android:name="android.intent.action.EDIT" />
               <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                  <data android:scheme="content" />
                  <data android:mimeType="text/x-vcard" />
           </intent-filter>
       <intent-filter android:label="Open iLocation">
           <action android:name="android.intent.action.VIEW" />
           <action android:name="android.intent.action.EDIT" />
           <category android:name="android.intent.category.DEFAULT" />
           <data android:scheme="file" />
           <data android:mimeType="text/x-vcard" />
           <data android:pathPattern=".*\.vcf" />
           <data android:pathPattern=".*\..*\.vcf" />
           <data android:pathPattern=".*\..*\..*\.vcf" />
           <data android:pathPattern=".*\..*\..*\..*\.vcf" />
           <data android:pathPattern=".*\..*\..*\..*\..*\.vcf" />
           <data android:host="*" />
       </intent-filter>
   </activity>

That opens my .Home file but I'm not sure how I get the data from the file. Any help would greatly be appreciated.


回答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




回答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")
@Override
protected void onActivityResult(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 = new Intent(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")
private String getPathFromURI(Uri mUri) {
    String mDocId = DocumentsContract.getDocumentId(mUri);
    String[] mSplit = mDocId.split(":");
    return Environment.getExternalStorageDirectory() + File.separator + mSplit[1];
}



回答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.

private fun saveVcfFile(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
    }
}


来源:https://stackoverflow.com/questions/35880241/how-do-i-open-a-vcf-from-an-intent-filer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!