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.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
|
@ -88,7 +87,7 @@ public class AttachmentContentLoader extends AsyncTaskLoader<Attachment> {
|
||||||
|
|
||||||
cachedResultAttachment = sourceAttachment.deriveWithLoadComplete(file.getAbsolutePath());
|
cachedResultAttachment = sourceAttachment.deriveWithLoadComplete(file.getAbsolutePath());
|
||||||
return cachedResultAttachment;
|
return cachedResultAttachment;
|
||||||
} catch (IOException e) {
|
} catch (Exception e) {
|
||||||
Timber.e(e, "Error saving attachment!");
|
Timber.e(e, "Error saving attachment!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -46,63 +46,70 @@ public class AttachmentInfoLoader extends AsyncTaskLoader<Attachment> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Attachment loadInBackground() {
|
public Attachment loadInBackground() {
|
||||||
Uri uri = sourceAttachment.uri;
|
try {
|
||||||
String contentType = sourceAttachment.contentType;
|
Uri uri = sourceAttachment.uri;
|
||||||
|
String contentType = sourceAttachment.contentType;
|
||||||
|
|
||||||
long size = -1;
|
long size = -1;
|
||||||
String name = null;
|
String name = null;
|
||||||
|
|
||||||
ContentResolver contentResolver = getContext().getContentResolver();
|
ContentResolver contentResolver = getContext().getContentResolver();
|
||||||
|
|
||||||
Cursor metadataCursor = contentResolver.query(
|
Cursor metadataCursor = contentResolver.query(
|
||||||
uri,
|
uri,
|
||||||
new String[] { OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE },
|
new String[] { OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE },
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
null);
|
null);
|
||||||
|
|
||||||
if (metadataCursor != null) {
|
if (metadataCursor != null) {
|
||||||
try {
|
try {
|
||||||
if (metadataCursor.moveToFirst()) {
|
if (metadataCursor.moveToFirst()) {
|
||||||
name = metadataCursor.getString(0);
|
name = metadataCursor.getString(0);
|
||||||
size = metadataCursor.getInt(1);
|
size = metadataCursor.getInt(1);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
metadataCursor.close();
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
metadataCursor.close();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (name == null) {
|
if (name == null) {
|
||||||
name = uri.getLastPathSegment();
|
name = uri.getLastPathSegment();
|
||||||
}
|
}
|
||||||
|
|
||||||
String usableContentType = contentResolver.getType(uri);
|
String usableContentType = contentResolver.getType(uri);
|
||||||
if (usableContentType == null && contentType != null && contentType.indexOf('*') != -1) {
|
if (usableContentType == null && contentType != null && contentType.indexOf('*') != -1) {
|
||||||
usableContentType = contentType;
|
usableContentType = contentType;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (usableContentType == null) {
|
if (usableContentType == null) {
|
||||||
usableContentType = MimeTypeUtil.getMimeTypeByExtension(name);
|
usableContentType = MimeTypeUtil.getMimeTypeByExtension(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!sourceAttachment.allowMessageType && MimeUtility.isMessageType(usableContentType)) {
|
if (!sourceAttachment.allowMessageType && MimeUtility.isMessageType(usableContentType)) {
|
||||||
usableContentType = MimeTypeUtil.DEFAULT_ATTACHMENT_MIME_TYPE;
|
usableContentType = MimeTypeUtil.DEFAULT_ATTACHMENT_MIME_TYPE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (size <= 0) {
|
if (size <= 0) {
|
||||||
String uriString = uri.toString();
|
String uriString = uri.toString();
|
||||||
if (uriString.startsWith("file://")) {
|
if (uriString.startsWith("file://")) {
|
||||||
File f = new File(uriString.substring("file://".length()));
|
File f = new File(uriString.substring("file://".length()));
|
||||||
size = f.length();
|
size = f.length();
|
||||||
|
} else {
|
||||||
|
Timber.v("Not a file: %s", uriString);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Timber.v("Not a file: %s", uriString);
|
Timber.v("old attachment.size: %d", size);
|
||||||
}
|
}
|
||||||
} else {
|
Timber.v("new attachment.size: %d", size);
|
||||||
Timber.v("old attachment.size: %d", size);
|
|
||||||
}
|
|
||||||
Timber.v("new attachment.size: %d", size);
|
|
||||||
|
|
||||||
cachedResultAttachment = sourceAttachment.deriveWithMetadataLoaded(usableContentType, name, size);
|
cachedResultAttachment = sourceAttachment.deriveWithMetadataLoaded(usableContentType, name, size);
|
||||||
return cachedResultAttachment;
|
return cachedResultAttachment;
|
||||||
|
} catch (Exception e) {
|
||||||
|
Timber.e(e, "Error getting attachment meta data");
|
||||||
|
|
||||||
|
cachedResultAttachment = sourceAttachment.deriveWithLoadCancelled();
|
||||||
|
return cachedResultAttachment;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue