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

标签 android android-intent

我制作了一个应用程序,用户可以在其中打开相机并拍摄图像。现在我希望通过电子邮件直接共享该图像。为此我使用了 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/

相关文章:

适用于应用程序的基于 Android Tile 的 UI

Android:致命异常:java.lang.NoClassDefFoundError(三星 4.4.2)

java - 什么时候是在 android 中获取 View 尺寸的正确时间

Android - 如何在 Marshmallow 中打开默认手机应用程序屏幕

android - 发送任何短信显示图标显示消息在Andorid中是 "pending to networks"和 "delivered to network"

java - 安卓文件未下载

android - 在已安装的应用程序列表中找不到该应用程序

android - 如何从 IntentService 返回一个对象?

java - Android Intent打开新 Activity 但没有关闭旧 Activity

android - 如何通过与不同应用程序兼容的 Intent 显示图像