1

My code is as below -

 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[] {})); 

When I select WeChat and Line app from the chooser, nothing happens. Line app just displays an error that it is unable to share. WeChat doesn't give any message. Sharing works fine on Whatsapp and Hangouts using my code.

We can share images on WeChat and Line from Gallery. How does that work? Is it not a Send action intent?

  • "My code is as below" -- no, it is not, as that code will not compile. "How does that work?" -- `ACTION_SEND`. What is your `Uri`? Since you are using `FLAG_GRANT_READ_URI_PERMISSION`, are you using `FileProvider` or implementing your own streaming `ContentProvider`? – CommonsWare May 27 '15 at 14:12
  • Hi @CommonsWare, I am implementing my own Content provider. As my code is proprietary, I cannot publish it in its entirety. Uri is of the form content://.... and the image is stored in my app's directory. The weird part is that my code works on Hangouts, Whatsapp, FB messenger but fails for WeChat and Line. I am using ACTION_SEND intent in my code as well, but what is the reason that sharing doesnt work for me whereas it works for Gallery. – user1154309 May 30 '15 at 19:01
  • You could try blending in [my `LegacyCompatCursorWrapper`](https://github.com/commonsguy/cwac-provider#usage-legacycompatcursorwrapper) to your `query()` method in your `ContentProvider`, or otherwise try to implement [this pattern](http://stackoverflow.com/a/25020642/115145) for dealing with some poorly-written streaming `ContentProvider` clients. – CommonsWare May 30 '15 at 19:08
  • The mentioned pattern didnt work for me. My cursor already has a column name MediaStore.MediaColumns.DATA i.e. _data. If I store my file in SDCard, image can be shared without any glitches. But for privacy reasons, I would like to store it in my app's folder. – user1154309 Jun 03 '15 at 15:22
  • P.S. If I add .setClipData to my intent, atleast WeChat launches and asks me to select a contact (for sharing). But it doesnt share the image (just shows the chat window). Have you ever tested your app with WeChat? – user1154309 Jun 03 '15 at 15:27
  • Sorry, I do not use WeChat or Line. – CommonsWare Jun 03 '15 at 15:51
  • 1
    any news on this? to this day either line nor wechat are working with FileProvider – Mario Lenci Mar 24 '16 at 15:40

2 Answers2

0

I was having the same problem so I implemented the following :

shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_SUBJECT,sharesubject);
shareIntent.putExtra(Intent.EXTRA_TEXT, sharetext);
shareIntent.setType("image/jpeg");
shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(destination));
shareIntent.setPackage(info.activityInfo.packageName);

//destination is the name of the file containing the image to be shared.

Note : This works only for api 23 and below.

nexus
  • 119
  • 13
0

Building share intent by ShareCompat works to me

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")

I guess the key point is

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

which is specified in ShareCompat.IntentBuilder(Context)

Frank Wong
  • 23
  • 1
  • 5