Android发送带附件的电子邮件并不总是有效

pla*_*jim 6 gmail android android-intent android-contentprovider

我正在使用ContentProvider发送包含附件的电子邮件.

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

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

当它不起作用时,电子邮件停留在gmail中发送.这发生在2.3.3和4.0.1上,在gmail客户端打开邮件并点击发送按钮,因此经常导致消息几乎立即发送,但不是每次都发送.

使用Google云端硬盘打开Intent与gmail具有相同的行为.

到目前为止,使用内置的Exchange邮件客户端打开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);
Run Code Online (Sandbox Code Playgroud)

这是内容提供商

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;
}
Run Code Online (Sandbox Code Playgroud)

}

对于成功和"停滞"的电子邮件发送,以下日志消息由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
Run Code Online (Sandbox Code Playgroud)

小智 -4

找到了治疗方法:

  • 禁用 Gmail。
  • 重置为出厂默认设置。
  • 重新启动手机,就像关闭然后重新打开一样。
  • 启用 Gmail。

应该可以再次处理附件