Ok, I resolved this issue using Intent. This is the right way to do what I wanted. In this way I can share directly on facebook (avoiding show the choosing window).
private void initShareIntent(String type) {
boolean found = false;
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg");
// gets the list of intents that can be loaded.
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
if (!resInfo.isEmpty()){
for (ResolveInfo info : resInfo) {
if (info.activityInfo.packageName.toLowerCase().contains(type) ||
info.activityInfo.name.toLowerCase().contains(type) ) {
if(info.activityInfo.name.toLowerCase().contains("messenger")){
continue;
}
Log.i("tag", "_info_name_"+info.activityInfo.name);
share.putExtra(Intent.EXTRA_SUBJECT, "subject");
share.putExtra(Intent.EXTRA_TEXT, "your text");
Bitmap img= Blur.takeScreenShot(PlayActivity.this);
String pathofBmp = Images.Media.insertImage(getContentResolver(), img,"title", null);
Uri bmpUri = Uri.parse(pathofBmp);
share.putExtra(Intent.EXTRA_STREAM, bmpUri); // Optional, just if you wanna share an image.
share.setPackage(info.activityInfo.packageName);
found = true;
break;
}
}
if (!found)
return;
startActivity(Intent.createChooser(share, "Select"));
}
}
I modified the example proposed here. The problem with the previously proposed code is that if Facebook Messenger is installed on the device, this code select Facebook Messenger and not Facebook. In addition there is the code to retrieve the image uri from Bitmap.
The use is: initShareIntent("facebook")
or initShareIntent("twitter")
etc...