這種方法允許你共享數據單個應用程序。
public void share(String nameApp, String imagePath, String message) {
try {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_EMAIL,`enter code here`
new String[] { "[email protected]" });
share.putExtra(Intent.EXTRA_SUBJECT, "subject");
share.putExtra(Intent.EXTRA_TEXT, "compose mail");
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,
"Sample Photo");
targetedShare.putExtra(Intent.EXTRA_TEXT, message);
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) {
Log.v("VM",
"Exception while sending image on" + nameApp + " "
+ e.getMessage());
}
}
例如,您可以通過使用共享圖像gmail
。
File filePath = new File("your image path");
share("gmail",filePath.toString(),"your comment");
您還可以通過更改名稱來與其他應用程序共享。
share("facebook",filePath.toString(),"your comment");
share("twitter",filePath.toString(),"your comment");
您只能使用ACTION_SENDTO常量打開郵件客戶端。 – Amy