Select Image from Native Gallery and attach to Email

徘徊边缘 提交于 2019-12-08 01:28:33

问题


I am allowing user to browse native gallery > select image, and trying to attach selected image to email, but not getting exact image, see my code:

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:"));       
}
}

What i am missing ? where i am doing mistake ?


回答1:


Try this for attaching image.

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

Hope it helps.



来源:https://stackoverflow.com/questions/20989855/select-image-from-native-gallery-and-attach-to-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!