'Android App: Saving image and attaching it to email with JavaMail
I am building an Android app where I would like to take a photo and save it to the device and then attach the said photo to an email with the JavaMail API and send it.
The application works in the Android Studio emulator and on my Huawei device but doesn't seem to work on Samsung phones and I'm guessing it won't work on others also. Is there a way of saving the photo somewhere that will work on all devices?
Below is the function I have for saving the photo to a directory called AppImages in Pictures.
private Uri createImage() {
Uri uri;
ContentResolver resolver = getContentResolver();
uri = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
String imgName = "newImage";
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, imgName + ".jpg");
contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/" + "AppImages");
Uri finalUri = resolver.insert(uri, contentValues);
imageUri = finalUri;
return finalUri;
}
This is where I attach the photo to an email using the JavaMail API.
FileDataSource source = new FileDataSource(new File("sdcard/Pictures/AppImages/newImage.jpg"));
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("Image - " + currentTime.toString() + ".jpg");
messageBodyPart.setDisposition(MimeBodyPart.INLINE);
messageBodyPart.setHeader("Content-ID","<test>");
As mentioned previously this works on the Android Emulator and on my personal Huawei device but I need a way of making this work on all devices. Any help would be appreciated!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
