1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-12-25 00:58:50 -05:00

Refactored AttachmentProvider.openFile()

This commit is contained in:
cketti 2012-01-24 14:34:32 +01:00
parent c69e6dc97c
commit 7a3cadbf1c

View File

@ -157,24 +157,24 @@ public class AttachmentProvider extends ContentProvider {
@Override @Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
File file;
List<String> segments = uri.getPathSegments(); List<String> segments = uri.getPathSegments();
String dbName = segments.get(0); // "/sdcard/..." is URL-encoded and makes up only 1 segment String accountUuid = segments.get(0);
String id = segments.get(1); String attachmentId = segments.get(1);
String format = segments.get(2); String format = segments.get(2);
if (FORMAT_THUMBNAIL.equals(format)) { if (FORMAT_THUMBNAIL.equals(format)) {
int width = Integer.parseInt(segments.get(3)); int width = Integer.parseInt(segments.get(3));
int height = Integer.parseInt(segments.get(4)); int height = Integer.parseInt(segments.get(4));
String filename = "thmb_" + dbName + "_" + id + ".tmp";
int index = dbName.lastIndexOf('/'); String filename = "thmb_" + accountUuid + "_" + attachmentId + ".tmp";
if (index >= 0) {
filename = /*dbName.substring(0, index + 1) + */"thmb_" + dbName.substring(index + 1) + "_" + id + ".tmp";
}
File dir = getContext().getCacheDir(); File dir = getContext().getCacheDir();
File file = new File(dir, filename); file = new File(dir, filename);
if (!file.exists()) { if (!file.exists()) {
String type = getType(dbName, id, FORMAT_VIEW); String type = getType(accountUuid, attachmentId, FORMAT_VIEW);
try { try {
FileInputStream in = new FileInputStream(getFile(dbName, id)); FileInputStream in = new FileInputStream(getFile(accountUuid, attachmentId));
try { try {
Bitmap thumbnail = createThumbnail(type, in); Bitmap thumbnail = createThumbnail(type, in);
if (thumbnail != null) { if (thumbnail != null) {
@ -187,18 +187,17 @@ public class AttachmentProvider extends ContentProvider {
} }
} }
} finally { } finally {
try { in.close(); } catch (Throwable ignore) {} try { in.close(); } catch (Throwable ignore) { /* ignore */ }
} }
} catch (IOException ioe) { } catch (IOException ioe) {
return null; return null;
} }
} }
return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
} else { } else {
return ParcelFileDescriptor.open( file = getFile(accountUuid, attachmentId);
getFile(dbName, id),
ParcelFileDescriptor.MODE_READ_ONLY);
} }
return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
} }
@Override @Override