我尝试使用FileProvider向电子邮件添加多个附件,如Android文档here中所述,并且找到了here的SO帖子。
问题是我不断收到来自Gmail的错误说明"由于文件大小为零而无法附加一个或多个文件"但是,我已经准确地遵循了文档:
<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>
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="share" path="/" />
</paths>
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;
}
奇怪的部分是我的uriList包含我要发送的文件的所有有效URI路径 - Gmail根本不允许他们附加 - 所以我认为这是某种问题FileProvider - 但我真的不知道。
非常感谢任何建议。
答案 0 :(得分:0)
当您真正想要imageDirectoryPath + fileList[i]
时,/sdcard/DirnameFilename
可能会创建类似/sdcard/Dirname/Filename
的字符串。