Wysyłam wiadomość e-mail z załącznikami za pomocą obiektu ContentProvider.Wysyłanie wiadomości e-mail z załącznikami Android nie zawsze działa
- Po pierwsze, zapisuję pliki w katalogu pamięci podręcznej.
- Następnie tworzę wiadomość e-mail z adresem URL dla każdego pliku znalezionego przez dostawcę treści
- Następnie rozpoczynam nową aktywność z intencją ACTION_SEND_MULTIPLE.
- Wybieram Gmail, a następnie naciśnij przycisk wysyłania.
To czasami działa, wydaje się działać po raz pierwszy od jakiegoś czasu, ale nie działa po kolejnych próbach ... ale nie zawsze tak jest.
Kiedy to nie zadziała, e-mail utknął w wysyłaniu wiadomości w Gmailu. Zdarza się to w wersjach 2.3.3 i 4.0.1, otwierając pocztę w kliencie Gmaila i naciskając przycisk Wyślij tak często powoduje, że wiadomość jest dostarczana niemal natychmiast, ale nie za każdym razem.
Otwarcie intencji za pomocą Dysku Google działa tak samo jak Gmail.
Otwarcie intencji za pomocą wbudowanego klienta pocztowego zawsze działa do tej pory.
Oto kod do wysyłania e-mail:
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);
Tutaj jest dostawcą treści
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;
}
}
Dla obu udany i 'utknęły' email wysyła następujący komunikat dziennika jest drukowane przez Gmaila:
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
Ten problem nie działa, jeśli dołączam unikalny ciąg do każdej nazwy pliku. Nie to jednak chcę robić. – placebojim
Tak. Właściwie to nie całkiem znika, staje się mniej prawdopodobne. – placebojim
Hej, udało ci się rozwiązać ten problem? Występuje coś podobnego, ale w moim przypadku e-maile są wysyłane, ale brakuje załączników. Otrzymuję te same komunikaty dziennika, ale rozmiary są poprawne. – Pin