【问题标题】:Android sending email with attachments doesn't always workAndroid 发送带附件的电子邮件并不总是有效
【发布时间】:2013-03-24 19:45:57
【问题描述】:

我正在使用 ContentProvider 发送带有附件的电子邮件。

  1. 首先,我将文件写入缓存目录。
  2. 然后我创建电子邮件,其中包含内容提供商可以找到的每个文件的 url
  3. 然后我使用 ACTION_SEND_MULTIPLE 意图启动一个新活动。
  4. 我选择 gmail,然后点击发送按钮。

这有时会起作用,它似乎在一段时间内第一次起作用,但在随后的尝试后不起作用......但并不总是这样。

当它不起作用时,电子邮件会卡在 gmail 中发送。这发生在 2.3.3 和 4.0.1 上,在 gmail 客户端打开邮件并点击发送按钮经常会导致邮件几乎立即发送,但并非每次都发送。

使用 Google Drive 打开 Intent 的行为与 gmail 相同。

到目前为止,使用内置交换邮件客户端打开 Intent 始终有效。

这是发送电子邮件的代码:

            Intent sendIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
            sendIntent.putExtra(Intent.EXTRA_EMAIL, exportParams.emailAddresses);
            sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Leader Activity Report");
            sendIntent.putExtra(Intent.EXTRA_TEXT, "Leader Activity Report, see attached file.");
            Uri fileUri = CachedFileProvider.createFileUri(result.fileName);
            if (L.dbg())
                L.dbg("Using uri:" + fileUri.toString());
            ArrayList<Uri> uris = new ArrayList<Uri>();
            uris.add(fileUri);
            Uri fileUri2 = CachedFileProvider.createFileUri(result.fileNameDayByDay);
            uris.add(fileUri2);
            if (L.dbg())
                L.dbg("Using uri2:" + fileUri2.toString());
            sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
            sendIntent.setType("text/plain");
            parent.startActivity(sendIntent);

这里是内容提供者

public class CachedFileProvider extends ContentProvider {

private static final String CLASS_NAME = "CachedFileProvider";
public static final String AUTHORITY = "com.josh.lll.file.provider";

private UriMatcher uriMatcher;

@Override
public boolean onCreate() {
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    uriMatcher.addURI(AUTHORITY, "*", 1);
    return true;
}


@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
        throws FileNotFoundException {
    try {
        String LOG_TAG = CLASS_NAME + " - openFile";
        Log.v(LOG_TAG,
                "Called with uri: '" + uri + "' - " + uri.getLastPathSegment());
        switch (uriMatcher.match(uri)) {
        case 1:
            String fileLocation = getContext().getCacheDir() + File.separator
                    + uri.getLastPathSegment();
            Log.i(CLASS_NAME,"Returning file :"+fileLocation);
            ParcelFileDescriptor pfd = ParcelFileDescriptor.open(new File(
                    fileLocation), ParcelFileDescriptor.MODE_READ_ONLY);
            return pfd;
        default:
            Log.v(LOG_TAG, "Unsupported uri: '" + uri + "'.");
            throw new FileNotFoundException("Unsupported uri: "
                    + uri.toString());
        }
    } catch (FileNotFoundException t) {
        Bug.major(this, t, "Could not return file descriptor");
        throw t;
    } catch (RuntimeException t) {
        Bug.major(this, t, "Could not return file descriptor");
        throw t;
    } catch (Error t) {
        Bug.major(this, t, "Could not return file descriptor");
        throw t;
    }
}

public static String createFullyQualifiedFileName(Context c, String fileNamePart) {
    File cacheDir = c.getCacheDir();
    Log.i(CLASS_NAME,"Using cache dir:"+cacheDir);
    return cacheDir + File.separator + fileNamePart;
}

public static Uri createFileUri(String fileNamePart) {
    return Uri.parse("content://" + AUTHORITY + "/"+ fileNamePart);
}

public int update(Uri uri, ContentValues contentvalues, String s,
        String[] as) {
    return 0;
}

@Override
public int delete(Uri uri, String s, String[] as) {
    return 0;
}

@Override
public Uri insert(Uri uri, ContentValues contentvalues) {
    return null;
}

@Override
public String getType(Uri uri) {
    return null;
}

@Override
public Cursor query(Uri uri, String[] projection, String s, String[] as1,
        String s1) {
    return null;
}

}

对于成功和“停滞”的电子邮件,gmail 会打印以下日志消息:

04-03 22:17:35.027: I/Gmail(13206): >>>>> Attachment uri: content://com.josh.lll.file.provider/report_20100401_20130402_LeadetJosh_3_1364980653516.csv
04-03 22:17:35.035: I/Gmail(13206): >>>>>           type: text/plain
04-03 22:17:35.035: I/Gmail(13206): >>>>>           size: 0
04-03 22:17:35.054: I/Gmail(13206): >>>>> Attachment uri:  content://com.josh.lll.file.provider/backup_20100401_20130402_LeadetJosh_3_1364980653516_day_by_day.lll
04-03 22:17:35.054: I/Gmail(13206): >>>>>           type: text/plain
04-03 22:17:35.054: I/Gmail(13206): >>>>>           size: 0

【问题讨论】:

  • 如果我在每个文件名上附加一个唯一的字符串,这个问题就会消失。但这不是我想做的。
  • 是的。实际上它并没有完全消失,只是可能性降低了。
  • 嘿,你解决了这个问题吗?我遇到了类似的情况,但在我的情况下,电子邮件已发送但附件丢失。我收到相同的日志消息,但大小是正确的。

标签: android android-intent gmail android-contentprovider


【解决方案1】:

这种情况发生在存储在 /data/app 或 /system 等系统文件夹中的文件。

解决方法是:将这些文件复制到 sdcard 位置并从那里附加/使用它们。

【讨论】:

    【解决方案2】:

    找到治疗方法:

    • 禁用 Gmail。
    • 重置为出厂默认设置。
    • 重启手机,如关闭再打开。
    • 启用 Gmail。

    应该再次适用于附件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-24
      • 2015-09-09
      • 2014-04-23
      • 2012-06-15
      • 1970-01-01
      相关资源
      最近更新 更多