【问题标题】:Android - Unable To Add Email Attachments Using FileProviderAndroid - 无法使用 FileProvider 添加电子邮件附件
【发布时间】:2017-12-25 12:01:33
【问题描述】:

我正在尝试使用 Android 文档 here 中记录的 FileProvider 将多个附件添加到电子邮件中,并且找到了一个 SO 帖子 here

问题是我不断从 Gmail 中收到一条错误消息,指出“由于文件大小为零,无法附加一个或多个文件”,但我已经严格按照文档进行操作:

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>

文件路径.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;
}

奇怪的部分是我的 uriList 包含我要发送的文件的所有有效 URI 路径 - Gmail 根本不允许它们附加 - 所以我认为这是 FileProvider 的某种问题 - 但我真的不知道。

非常感谢任何建议。

【问题讨论】:

    标签: java android android-intent email-attachments android-fileprovider


    【解决方案1】:

    imageDirectoryPath + fileList[i] 可能会在您真正需要 /sdcard/Dirname/Filename 时创建类似 /sdcard/DirnameFilename 的字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2011-02-03
      • 1970-01-01
      相关资源
      最近更新 更多