代码:
private static void showShareViaDialog(final Context context, ArrayList<Uri> shareFileUris) {
if (context == null || shareFileUris == null) {
LOG.e(TAG, "Either context or Uri list object is null");
ToastView.makeCustomView(ContextHolder.getContext(), R.string.share_image_storage_error, Toast.LENGTH_LONG).show();
return;
}
Intent sendingIntent;
Intent chooserActivity;
if (shareFileUris.size() <= 0) {
LOG.e(TAG, "Uri list is empty");
return;
if (shareFileUris.size() == 1) {
LOG.d(TAG, " showShareViaDialog() : only one file is about to share ");
sendingIntent = new Intent(Intent.ACTION_SEND);
sendingIntent.setType("image/png");
sendingIntent.putExtra(Intent.EXTRA_STREAM, shareFileUris.get(0));
sendingIntent.putExtra(THEME_CHOOSER, THEME_DEVICE_DEFAULT_LIGHT);
sendingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
chooserActivity = Intent.createChooser(sendingIntent, context.getResources().getText(R.string.share_via));
if (chooserActivity != null) {
LOG.d(TAG, " showShareViaDialog() : chooser activity is " + chooserActivity.getClass());
chooserActivity.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_GRANT_READ_URI_PERMISSION);
LockManager.getInstance().registerIgnoreActivity(context.getClass());
List<ResolveInfo> resolveInfoList = context.getPackageManager().queryIntentActivities(chooserActivity,
PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resolveInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
context.grantUriPermission(packageName, shareFileUris.get(0), Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
LOG.d(TAG, " showShareViaDialog() : chooser activity is starting " + chooserActivity.getClass());
context.startActivity(chooserActivity);
}
意图发送部分代码
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/png" />
</intent-filter>
AndroidManifest.xml中的Intent-filter
起初,我只是认为它与Android Oreo Broadcast Limitations有关。但是,action.SEND是活动Intent,而不是广播Intent。我发现了我的一些相反的案例问题Why is my ACTION_SEND working on API 25+ perfectly, but messes up on previous APIs?。这很有趣。
为什么此流程作业在API 26中不起作用?是否有更多的事情要做以接受行动。在API 26中的意图?
++更多信息,
shareFileUris.get(0)。我们不知道您尝试分享的内容。请 给出Uri.toString()的值。 - greenapps 14分钟前
这是一个指示PNG类型图像文件的uri。请参考以下代码。
Uri shareUri;
if (android.os.Build.VERSION.SDK_INT > ANDROID_BUILD_VERSION_LOLLIPOP) {
shareUri = FileProvider.getUriForFile(SHARE_VIA_AUTHORITY, file);
LOG.d(TAG, " doInBackground(): share Uri through FileProvider " + shareUri.toString());
} else {
shareUri = Uri.fromFile(file);
LOG.d(TAG, " doInBackground(): share Uri through Uri apis " + shareUri.toString());
}
// context.grantUriPermission();
// Uri shareUri = Uri.fromFile(file);
shareUris = new ArrayList<>();
shareUris.add(shareUri);
public static Uri getUriForFile(String authority, File file) {
return Uri.parse("content://" + authority + "/" + file.getName());
}