无法在android中附加文件

时间:2013-06-21 19:44:41

标签: android android-intent email-attachments android-image android-implicit-intent

我想在邮件中附加文件....事情是我想使用ACTION_SENDTO。当我发送没有附件的邮件时它工作正常但是当我尝试附加文件时它会给出一个异常(android.content.ActivityNotFoundException)就像以下链接一样: How to launch email intent with an attached image

我尝试了答案,但未解决问题。 这是我的代码

Uri mail= Uri.fromParts("mailto",message, null);                                
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, mail);                
emailIntent.putExtra(Intent.EXTRA_SUBJECT, sub);
emailIntent.putExtra(Intent.EXTRA_TEXT, mailcontent);               
emailIntent.setData(Uri.parse("file://" + Environment.getExternalStorageDirectory()+"/ab.jpg"));
emailIntent.setType("image/jpg"); 
startActivity(emailIntent);

3 个答案:

答案 0 :(得分:0)

请使用Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent .putExtra(Intent.EXTRA_STREAM, uri);

答案 1 :(得分:0)

需要使用EXTRA_STREAM

        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

        Uri outputFileUri = Uri.fromFile( file );
        emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, outputFileUri);

答案 2 :(得分:0)

试试这个:

    private static final String MESSAGE_RFC822 = "message/rfc822";
    private static final String SELECT_EMAIL_APPLICATION = "Select email application.";

    public static void sendEmail(Context theContext, final String title, final String[] emails, final String theSubject, final String theBody,
        final File file) throws ActivityNotFoundException
{
    final Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType(MESSAGE_RFC822);
    intent.putExtra(Intent.EXTRA_EMAIL, emails);
    intent.putExtra(Intent.EXTRA_SUBJECT, theSubject);
    intent.putExtra(Intent.EXTRA_TEXT, theBody);
    if (file != null) {
        intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.getAbsolutePath()));
    }

    theContext.startActivity(Intent.createChooser(intent, SELECT_EMAIL_APPLICATION));
}