从牛轧糖和奥利奥版本的"文件"应用接收.vcf文件到我的应用



我有一个Android应用程序,在Jelly Bean和Kitkat版本中运行良好。该应用程序将使用">完成操作使用"选项从文件管理器应用程序接收.vcf文件作为Intent文件。

现在在Android Nougat和Oreo版本中,使用文件应用程序,有一个选项打开方式共享不会列出我的应用程序。

如何解决这个问题?提前谢谢。

安卓清单.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app" >
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme.CustomOrange"
>
<activity>
...
...
</activity>
<activity
android:name=".ViewContactActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/title_activity_vcf" >
<intent-filter> <!-- Handle http https requests without mimeTypes: -->
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="*" />
<data android:pathPattern="/.*\.vcf" />
</intent-filter>
<intent-filter> <!-- Handle with mimeTypes, where the suffix is irrelevant: -->
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="*" />
<data android:mimeType="text/x-vcard" />
</intent-filter>
<intent-filter> <!-- Handle intent from a file browser app: -->
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.INSERT_OR_EDIT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="file" />
<data android:host="*" />
<data android:mimeType="text/x-vcard" />
</intent-filter>
</activity>
</application>
</manifest>

查看联系人活动.java

public  class       ViewContactActivity
extends     AppCompatActivity {
private static final String TAG = ViewContactActivity.class.getSimpleName();
public static final String  VIEW_CONTACT_BY_ID      = "viewContactById";
private static final int    MODE_VIEW_BY_ID     = 0;
private static final int    MODE_VIEW_BY_FILE   = 1;
private int                 viewContactMode;
private ActionBar mActionBar;
ContactDetails contactDetails;
String mFilePath;
private GroupInfoPickerAlertDialog mGroupInfoPickerAD;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vcf);

contactDetails = new ContactDetails(this);
Intent intent = getIntent();
Long id = intent.getLongExtra(VIEW_CONTACT_BY_ID, -1L);
Bitmap bitmap = null;
if (id != -1L) {
viewContactMode = MODE_VIEW_BY_ID;
contactDetails.collectContactDetail(id);
bitmap = BitmapFactory.decodeStream(new BufferedInputStream(contactDetails.getDisplayPhoto()));
} else {
viewContactMode = MODE_VIEW_BY_FILE;
Uri fileUri = intent.getData();
mFilePath = fileUri.getPath();
Log.d(TAG, "onCreate() - file path: " + mFilePath);
//onCreate() - file path: /path/to/file.vcf
contactDetails.readVCard2_1(fileUri);
}

setSupportActionBar((Toolbar)findViewById(R.id.toolbar));
mActionBar = getSupportActionBar();
if(mActionBar == null) {
Log.d(TAG, "onCreate() mActionBar == null");
} else {
Log.d(TAG, "onCreate() mActionBar != null");
}
mActionBar.setCustomView(null);
mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);

contactDetails.displayContact(bitmap);
if (viewContactMode == MODE_VIEW_BY_FILE) {
TextView textView = (TextView) findViewById(R.id.message_text_view);
textView.setText(mFilePath);// + "nn" + stringBuilder);
} else {
View v = (View) findViewById(R.id.message_text_view).getParent();
v.setVisibility(View.GONE);
}
}
// ....
// ....
// Other codes...
// ....
// ....
}

您的<intent-filter>元素支持httphttpsfile。很少有"文件管理器"应用程序会在Android 7.0 +上使用任何应用程序,部分原因是file方案被禁止。

取代:

<intent-filter> <!-- Handle intent from a file browser app: -->
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.INSERT_OR_EDIT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="file" />
<data android:host="*" />
<data android:mimeType="text/x-vcard" />
</intent-filter>

跟:

<intent-filter> <!-- Handle intent from a file browser app: -->
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.INSERT_OR_EDIT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="file" />
<data android:scheme="content" />
<data android:mimeType="text/x-vcard" />
</intent-filter>

然后,确保readVCard2_1()处理filecontentUri方案,例如通过在ContentResolver上调用openInputStream()来访问流。

最新更新