2

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 ?

Sun
  • 6,768
  • 25
  • 76
  • 131
  • print `picturePath` value and check what it holds ? – Kitkat Jan 08 '14 at 08:01
  • @Kitkat i tried using this: Toast.makeText(getApplicationContext(), picturePath.toString(), Toast.LENGTH_LONG).show(); and getting exact image name which i selected – Sun Jan 08 '14 at 08:16
  • are you getting only image name or image name with full path ? – Kitkat Jan 08 '14 at 08:17
  • @Kitkat getting full path like:- storage > sdcard0 > dcim > Camera > copy2.jpg – Sun Jan 08 '14 at 08:51

1 Answers1

2

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.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124