从Native Gallery中选择Image并附加到Email

时间:2014-01-08 07:53:43

标签: android email android-intent android-gallery

我允许用户浏览原生图库>选择图像,并尝试将所选图像附加到电子邮件,但没有获得确切的图像,请参阅我的代码:

private static int RESULT_LOAD_IMAGE = 1;
String picturePath;    

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        picturePath = cursor.getString(columnIndex);
        cursor.close();

        // getting exact image which i selected with full path
        Toast.makeText(getApplicationContext(), picturePath.toString(), Toast.LENGTH_LONG).show();

        sendImage();

    }
}

// to attach an image to email
public void sendImage() {
    // TODO Auto-generated method stub
    Intent i = new Intent(Intent.ACTION_SEND);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.setType("message/rfc822");
    i.putExtra(Intent.EXTRA_STREAM, Uri.parse(picturePath));
    i.putExtra(Intent.EXTRA_EMAIL, new String[]{"recepient@mail.cin"});
    i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
    i.putExtra(Intent.EXTRA_TEXT, "body of email");
    startActivity(Intent.createChooser(i,"Send email via:"));       
}
}

我缺少什么?我在哪里做错了?

1 个答案:

答案 0 :(得分:2)

尝试附加图像。

File file = getFileStreamPath(EMAIL_TEMP_FILE);
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.setType("image/jpeg");
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+file.getAbsolutePath())); //Your image file

希望它有所帮助。