From f9f439feb0fb545e956d5ad57b0b3e1569495aad Mon Sep 17 00:00:00 2001 From: Fredrik Fornwall Date: Sat, 28 Nov 2015 23:38:34 +0100 Subject: [PATCH] Improve ShareAPI to work in more cases --- .../main/java/com/termux/api/ShareAPI.java | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/termux/api/ShareAPI.java b/app/src/main/java/com/termux/api/ShareAPI.java index 7f0411cc..808b9da9 100644 --- a/app/src/main/java/com/termux/api/ShareAPI.java +++ b/app/src/main/java/com/termux/api/ShareAPI.java @@ -17,8 +17,6 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; -import java.lang.reflect.Array; -import java.util.Arrays; public class ShareAPI { @@ -131,8 +129,36 @@ public Cursor query(Uri uri, String[] projection, String selection, String[] sel File file = new File(uri.getPath()); String fileName = file.getName(); - MatrixCursor cursor = new MatrixCursor(new String[]{MediaStore.MediaColumns.DISPLAY_NAME}); - cursor.addRow(new Object[]{fileName}); + if (projection == null) { + projection = new String[]{ + MediaStore.MediaColumns.DISPLAY_NAME, + MediaStore.MediaColumns.SIZE, + MediaStore.MediaColumns._ID + }; + } + + Object[] row = new Object[projection.length]; + for (int i = 0; i < projection.length; i++) { + String column = projection[i]; + Object value; + switch (column) { + case MediaStore.MediaColumns.DISPLAY_NAME: + value = file.getName(); + break; + case MediaStore.MediaColumns.SIZE: + value = (int) file.length(); + break; + case MediaStore.MediaColumns._ID: + value = 1; + break; + default: + value = null; + } + row[i] = value; + } + + MatrixCursor cursor = new MatrixCursor(projection); + cursor.addRow(row); return cursor; }