Ahmad Aghazadeh
Ahmad Aghazadeh

Reputation: 17131

When sharing text and image errors occur in the various versions of Android

Code is written as follows:

        final Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("*/*");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(Intent.EXTRA_SUBJECT, subject.toString());
        intent.putExtra(Intent.EXTRA_TEXT, text.toString());
        intent.putExtra(Intent.EXTRA_STREAM, "file://" + path);

         startActivityForResult(
                Intent.createChooser(intent, title.toString()), REQUEST_CODE);

The results are as follows

  1. API 10 : work
  2. API 14 : error

Upvotes: 1

Views: 350

Answers (2)

Ahmad Aghazadeh
Ahmad Aghazadeh

Reputation: 17131

final Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("*/*");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra(Intent.EXTRA_SUBJECT, subject.toString());
    intent.putExtra(Intent.EXTRA_TEXT, text.toString());
    intent.putExtra(Intent.EXTRA_STREAM,
            Uri.fromFile(new File("/" + path)));

Upvotes: 1

Ajay S
Ajay S

Reputation: 48592

Use this method to share photo with text. Call this method and pass argument nameofapp and imagepath. Name of app means like on which you want to share like gmail , facebook , twitter.

private void share(String nameApp, String imagePath) {
    try
    {
        List<Intent> targetedShareIntents = new ArrayList<Intent>();
        Intent share = new Intent(android.content.Intent.ACTION_SEND);
        share.setType("image/jpeg");
        List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
        if (!resInfo.isEmpty()){
            for (ResolveInfo info : resInfo) {
                    Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND);
                    targetedShare.setType("image/jpeg"); // put here your mime type
                    if (info.activityInfo.packageName.toLowerCase().contains(nameApp) || info.activityInfo.name.toLowerCase().contains(nameApp)) {
                        targetedShare.putExtra(Intent.EXTRA_SUBJECT, "Text");
                        targetedShare.putExtra(Intent.EXTRA_TEXT,"This photo is created by APP");
                        targetedShare.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(imagePath)) );
                        targetedShare.setPackage(info.activityInfo.packageName);
                        targetedShareIntents.add(targetedShare);
                    }
                }
                Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
                chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
                startActivity(chooserIntent);
        }
    }
    catch(Exception e){
    }
 }

Use this like :-

try {
    File filePath = "/mnt/sdcard/vmphoto.jpg"; //This is imagefile path in your change it acc. to your requirement.
    share("facebook",filePath.toString());

}
catch(Exception e) {
           //exception occur might your app like gmail , facebook etc not installed or not working correctly.
}

Upvotes: 2

Related Questions