无法使用ACTION_SEND意图将图像分享到微信和线路

时间:2015-05-27 14:01:39

标签: android android-intent wechat

我的代码如下 -

 Intent prototype = new Intent(Intent.ACTION_SEND);
 prototype.setData(uri);   // uri is of the image file
 prototype.setType(image/*);
 prototype.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

 prototype.putExtra(Intent.EXTRA_STREAM, uri); 

List<ResolveInfo> resInfo = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
       if (!resInfo.isEmpty()) 
       { 
           for (ResolveInfo resolveInfo : resInfo) 
            {
                if (resolveInfo.activityInfo == null)  
                {
                    continue; 
                }

                // Retrieve the package name and class name to populate the chooser intent
                Intent intent = (Intent) prototype.clone();
                String packageName = resolveInfo.activityInfo.packageName;
                intent.setPackage(packageName); 
                intent.setClassName(packageName, resolveInfo.activityInfo.name); 
            targetedIntents.add(intent);
        }}
            Intent chooserIntent = Intent.createChooser(topIntents.remove(targetedIntents.size() - 1), chooserTitle); 
                chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedIntents.toArray(new Parcelable[] {})); 

当我从选择器中选择微信和线路应用时,没有任何反应。 Line应用程序只显示无法共享的错误。微信没有给出任何信息。使用我的代码,Whatsapp和Hangouts上的共享工作正常。

我们可以在微信和画廊线上分享图像。这是如何运作的?这不是发送动作意图吗?

1 个答案:

答案 0 :(得分:0)

通过 ShareCompat 建立分享意图对我有用

val shareIntent = ShareCompat.IntentBuilder(context)
    .setType(context.contentResolver.getType(uri))
    .setStream(uri)
    .setText(text)
    .intent
    .apply { addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) }

startActivity(Intent.createChooser(shareIntent, "chooserTitle")

我想关键是

mIntent.putExtra(EXTRA_CALLING_PACKAGE, launchingContext.getPackageName());

ShareCompat.IntentBuilder(Context)

中指定