Merge pull request #6255 from thundernest/fix_shared_attachment_crash
Don't crash when trying to access attachment (meta) data
This commit is contained in:
commit
1de1f54518
2 changed files with 52 additions and 46 deletions
|
@ -3,7 +3,6 @@ package com.fsck.k9.activity.loader;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
|
@ -88,7 +87,7 @@ public class AttachmentContentLoader extends AsyncTaskLoader<Attachment> {
|
|||
|
||||
cachedResultAttachment = sourceAttachment.deriveWithLoadComplete(file.getAbsolutePath());
|
||||
return cachedResultAttachment;
|
||||
} catch (IOException e) {
|
||||
} catch (Exception e) {
|
||||
Timber.e(e, "Error saving attachment!");
|
||||
}
|
||||
|
||||
|
|
|
@ -46,63 +46,70 @@ public class AttachmentInfoLoader extends AsyncTaskLoader<Attachment> {
|
|||
|
||||
@Override
|
||||
public Attachment loadInBackground() {
|
||||
Uri uri = sourceAttachment.uri;
|
||||
String contentType = sourceAttachment.contentType;
|
||||
try {
|
||||
Uri uri = sourceAttachment.uri;
|
||||
String contentType = sourceAttachment.contentType;
|
||||
|
||||
long size = -1;
|
||||
String name = null;
|
||||
long size = -1;
|
||||
String name = null;
|
||||
|
||||
ContentResolver contentResolver = getContext().getContentResolver();
|
||||
ContentResolver contentResolver = getContext().getContentResolver();
|
||||
|
||||
Cursor metadataCursor = contentResolver.query(
|
||||
uri,
|
||||
new String[] { OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE },
|
||||
null,
|
||||
null,
|
||||
null);
|
||||
Cursor metadataCursor = contentResolver.query(
|
||||
uri,
|
||||
new String[] { OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE },
|
||||
null,
|
||||
null,
|
||||
null);
|
||||
|
||||
if (metadataCursor != null) {
|
||||
try {
|
||||
if (metadataCursor.moveToFirst()) {
|
||||
name = metadataCursor.getString(0);
|
||||
size = metadataCursor.getInt(1);
|
||||
if (metadataCursor != null) {
|
||||
try {
|
||||
if (metadataCursor.moveToFirst()) {
|
||||
name = metadataCursor.getString(0);
|
||||
size = metadataCursor.getInt(1);
|
||||
}
|
||||
} finally {
|
||||
metadataCursor.close();
|
||||
}
|
||||
} finally {
|
||||
metadataCursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
if (name == null) {
|
||||
name = uri.getLastPathSegment();
|
||||
}
|
||||
if (name == null) {
|
||||
name = uri.getLastPathSegment();
|
||||
}
|
||||
|
||||
String usableContentType = contentResolver.getType(uri);
|
||||
if (usableContentType == null && contentType != null && contentType.indexOf('*') != -1) {
|
||||
usableContentType = contentType;
|
||||
}
|
||||
String usableContentType = contentResolver.getType(uri);
|
||||
if (usableContentType == null && contentType != null && contentType.indexOf('*') != -1) {
|
||||
usableContentType = contentType;
|
||||
}
|
||||
|
||||
if (usableContentType == null) {
|
||||
usableContentType = MimeTypeUtil.getMimeTypeByExtension(name);
|
||||
}
|
||||
if (usableContentType == null) {
|
||||
usableContentType = MimeTypeUtil.getMimeTypeByExtension(name);
|
||||
}
|
||||
|
||||
if (!sourceAttachment.allowMessageType && MimeUtility.isMessageType(usableContentType)) {
|
||||
usableContentType = MimeTypeUtil.DEFAULT_ATTACHMENT_MIME_TYPE;
|
||||
}
|
||||
if (!sourceAttachment.allowMessageType && MimeUtility.isMessageType(usableContentType)) {
|
||||
usableContentType = MimeTypeUtil.DEFAULT_ATTACHMENT_MIME_TYPE;
|
||||
}
|
||||
|
||||
if (size <= 0) {
|
||||
String uriString = uri.toString();
|
||||
if (uriString.startsWith("file://")) {
|
||||
File f = new File(uriString.substring("file://".length()));
|
||||
size = f.length();
|
||||
if (size <= 0) {
|
||||
String uriString = uri.toString();
|
||||
if (uriString.startsWith("file://")) {
|
||||
File f = new File(uriString.substring("file://".length()));
|
||||
size = f.length();
|
||||
} else {
|
||||
Timber.v("Not a file: %s", uriString);
|
||||
}
|
||||
} else {
|
||||
Timber.v("Not a file: %s", uriString);
|
||||
Timber.v("old attachment.size: %d", size);
|
||||
}
|
||||
} else {
|
||||
Timber.v("old attachment.size: %d", size);
|
||||
}
|
||||
Timber.v("new attachment.size: %d", size);
|
||||
Timber.v("new attachment.size: %d", size);
|
||||
|
||||
cachedResultAttachment = sourceAttachment.deriveWithMetadataLoaded(usableContentType, name, size);
|
||||
return cachedResultAttachment;
|
||||
cachedResultAttachment = sourceAttachment.deriveWithMetadataLoaded(usableContentType, name, size);
|
||||
return cachedResultAttachment;
|
||||
} catch (Exception e) {
|
||||
Timber.e(e, "Error getting attachment meta data");
|
||||
|
||||
cachedResultAttachment = sourceAttachment.deriveWithLoadCancelled();
|
||||
return cachedResultAttachment;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue