I'm trying to send an email with an attached file in an android app. The email appears to get sent but never shows up. It is stuck in the Outbox of Gmail with the message "Sending...." It does not get sent. I suspect that the FileProvider code is not getting set up correctly as an email with no attachment goes through.
public void emailSurvey()
{
int i;
final Intent intent;
int checkedCount = 0;
Context mContext = this;
//if (emailAddress.isEmpty())
// return;
ArrayList<Uri> uris = new ArrayList<>();
File externalDir = getExternalFilesDir(this.getPackageName());
String filePath = externalDir.toString() + "/surveys" + "/";
intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("application/zip");
intent.putExtra(Intent.EXTRA_EMAIL, emailAddress);
intent.putExtra(Intent.EXTRA_SUBJECT, "Survey Attached");
intent.putExtra(android.content.Intent.EXTRA_TEXT, "Surveys attached.");
for (i=0;i<arrayList.size();i++)
{
FileListItem fli = arrayList.get(i);
try {
if (fli.isChecked()) {
checkedCount++;
String fileData = fli.getFilename();
String[] filename = fileData.split(" ");
File checkedFile = new File(filePath + filename[0]);
Log.v(TAG, filePath + fli.getFilename());
checkedFile.setReadable(true, false);
final Uri u = FileProvider.getUriForFile(mContext,"com.example.mbietsch.andoidtrafficstat.provider",checkedFile);
mContext.grantUriPermission(mContext.getPackageName(),u,Intent.FLAG_GRANT_READ_URI_PERMISSION);
uris.add(u);
fli.setChecked(false);
}
}
catch (NullPointerException npe)
{
Log.e(TAG,"Off screen");
}
}
if (intent.resolveActivity(getPackageManager()) != null && checkedCount > 0)
{
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
mContext.startActivity(Intent.createChooser(intent,"Surveys"));
//startActivity(intent);
}
else if (checkedCount == 0)
{
Toast.makeText(this.getBaseContext(),"No Files Selected.",Toast.LENGTH_SHORT).show();
}
}
XML file
<external-path name="survey_files" path="Android/data/com.example.mbietsch.andoidtrafficstat/files/com.example.mbietsch.andoidtrafficstat/surveys"/>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.mbietsch.andoidtrafficstat.provider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
I would expect the email to get delivered. It does not if a file is attached. I'm assuming I have done something wrong with FileProvider and permission was not granted correctly. But with no error message, I'm stuck.