问题
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