仅通过电子邮件分享图片

时间:2014-09-16 06:15:40

标签: android android-intent

我已经制作了一个应用程序,其中用户打开相机并捕获图像。现在我希望该图像通过电子邮件直接共享。为此我使用了intents.But我的问题是当用户finsh捕获图像多个选项就像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();
            }

        }
    }

1 个答案:

答案 0 :(得分:1)

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

尝试类似的内容:

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);