I'm attempting to add multiple attachments to an email using a FileProvider as documented in the Android documentation here and a SO post found here.
The problem is I continuously get an error from Gmail stating "One or More files cannot be attached due to zero file size" however I've followed the documentation precisely:
AndroidManifest:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.collision.man.collisionman"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths">
</meta-data>
</provider>
FilePaths.xml:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="share" path="/" />
</paths>
SendActivity.java:
Intent sendIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
// sendIntent.setType("plain/text");
sendIntent.setType("image/*");
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[{"carl@collisionman.com"});
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "CollisionMan Damage Estimate");
sendIntent.putExtra(Intent.EXTRA_TEXT, emailBody);
ArrayList<Uri> uriList = null;
try {
uriList = getUriListForImages();
} catch (Exception e) {
e.printStackTrace();
}
sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);
Log.d("TAG", "Size of the ArrayList :: " + uriList.size());
SendActivity.this.startActivity(Intent.createChooser(sendIntent, "Email:"));
//}
}
private ArrayList<Uri> getUriListForImages() throws Exception {
ArrayList<Uri> uriList = new ArrayList<Uri>();
String imageDirectoryPath = getExternalStorageDirectory() + "/DirName";
File imageDirectory = new File(Environment.getExternalStorageDirectory() + "/DirName");
String[] fileList = imageDirectory.list();
if (fileList.length != 0) {
for (int i = 0; i < fileList.length; i++) {
try {
Uri u = FileProvider.getUriForFile(getApplicationContext(), "com.collision.man.collisionman", new File(imageDirectoryPath + fileList[i]));
uriList.add(u);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return uriList;
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
// File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES + picture_directory);
File storageDir = new File(getExternalStorageDirectory() + "/DirName");
if (!storageDir.exists()) {
File imageDirectory = new File("/sdcard/DirName/");
imageDirectory.mkdirs();
}
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
The strange part is my uriList contains all valid URI paths for the files I'd like to send - Gmail simply will not allow them to attach - so I'm thinking this is some sort of issue with the FileProvider - but I really have no idea.
Any suggestions are greatly appreciated.