我正在尝试在 Android 应用程序中发送带有附件的电子邮件。电子邮件似乎已发送,但从未出现。它卡在 Gmail 的发件箱中,并显示“正在发送...”消息,无法发送。我怀疑 FileProvider 代码没有正确设置,因为没有附件的电子邮件通过了。
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();
}
}
文件
<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>
我希望电子邮件能够送达。如果附加了文件,则不会。我假设我对 FileProvider 做错了,并且没有正确授予权限。但是没有错误消息,我被卡住了。