gpt4 book ai didi

android - 仅通过电子邮件共享图像

转载 作者:行者123 更新时间:2023-11-29 01:39:11 24 4
gpt4 key购买 nike

我制作了一个应用程序,用户可以在其中打开相机并拍摄图像。现在我希望通过电子邮件直接共享该图像。为此我使用了 intents。但我的问题是当用户完成多次拍摄图像时选项像 whatsap、google、gmail、hike 等。我只希望用户仅通过电子邮件而不是与其他应用程序共享图像

代码

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// if the result is capturing Image
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("image/jpg");
Uri myUri = Uri.parse("file://" + fileUri.getPath());
emailIntent.putExtra(Intent.EXTRA_STREAM, myUri);
startActivity(Intent.createChooser(emailIntent,
"Send mail..."));


} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(), "User cancelled image capture", Toast.LENGTH_SHORT).show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(), "Sorry! Failed to capture image", Toast.LENGTH_SHORT).show();
}

}
}

最佳答案

我建议过滤用户可用于共享文件的选项,并将其限制为仅电子邮件应用程序。

尝试类似的东西:

Intent emailIntent = new Intent();
emailIntent.setAction(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_TEXT, // email body);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, // email subject);
// Add Image to email

PackageManager pm = getPackageManager();
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("image/jpg");


Intent openInChooser = Intent.createChooser(emailIntent, resources.getString(R.string.share_chooser_text));

List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
for (int i = 0; i < resInfo.size(); i++) {
// Extract the label, append it, and repackage it in a LabeledIntent
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
if(packageName.contains("android.email")) {
emailIntent.setPackage(packageName);
}
intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
}
}

// convert intentList to array
LabeledIntent[] extraIntents = intentList.toArray( new LabeledIntent[ intentList.size() ]);

openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(openInChooser);

关于android - 仅通过电子邮件共享图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25861802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com