使用此方法与文字分享照片。调用此方法并传递参数 nameofapp 和 imagepath。应用程序名称表示您想要分享的内容,如 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){
}
}
像这样使用:-
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.
}