如何从意图文件管理器打开 .vcf?
How do I open a .vcf from an intent filer?
您好,我正在尝试制作一个可以打开 .vcf 文件的应用程序。主要来自短信应用程序,我已经让它工作,我可以使用以下代码打开我的应用程序和 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>
这会打开我的 .Home 文件,但我不确定如何从文件中获取数据。任何帮助将不胜感激。
要获取表示您要查看或编辑的内容的 Uri
,请调用 getIntent().getData()
。然后,您可以使用 ContentResolver
和 openInputStream()
在您提供查看或编辑的实际内容上获得 InputStream
,前提是 Uri
.
就 vCard 本身而言,Android 没有任何内置的 vCard 解析,就传统的 Java API 而言。您的选择是:
寻找另一个第三方库或其他 vCard 解析代码块(例如,通讯录应用应该在某处有一些)
滚动您自己的解析器足以满足您的需求
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);
}
并获得意向结果......
@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];
}
这其实不是一个很复杂的过程。只需确保您从 FileProvider 获得 Uri
,您就可以触发 Phone Book Chooser Intent。
然后,您就可以使用 .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
}
}
您好,我正在尝试制作一个可以打开 .vcf 文件的应用程序。主要来自短信应用程序,我已经让它工作,我可以使用以下代码打开我的应用程序和 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>
这会打开我的 .Home 文件,但我不确定如何从文件中获取数据。任何帮助将不胜感激。
要获取表示您要查看或编辑的内容的 Uri
,请调用 getIntent().getData()
。然后,您可以使用 ContentResolver
和 openInputStream()
在您提供查看或编辑的实际内容上获得 InputStream
,前提是 Uri
.
就 vCard 本身而言,Android 没有任何内置的 vCard 解析,就传统的 Java API 而言。您的选择是:
寻找另一个第三方库或其他 vCard 解析代码块(例如,通讯录应用应该在某处有一些)
滚动您自己的解析器足以满足您的需求
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);
}
并获得意向结果......
@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];
}
这其实不是一个很复杂的过程。只需确保您从 FileProvider 获得 Uri
,您就可以触发 Phone Book Chooser Intent。
然后,您就可以使用 .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
}
}