Use DocumentFile

This commit is contained in:
M66B 2019-11-02 19:56:52 +01:00
parent 5b3619c370
commit 6c58d25166
1 changed files with 28 additions and 22 deletions

View File

@ -90,6 +90,7 @@ import androidx.appcompat.widget.PopupMenu;
import androidx.constraintlayout.widget.Group; import androidx.constraintlayout.widget.Group;
import androidx.core.content.FileProvider; import androidx.core.content.FileProvider;
import androidx.cursoradapter.widget.SimpleCursorAdapter; import androidx.cursoradapter.widget.SimpleCursorAdapter;
import androidx.documentfile.provider.DocumentFile;
import androidx.lifecycle.Lifecycle; import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.Observer; import androidx.lifecycle.Observer;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
@ -1968,24 +1969,36 @@ public class FragmentCompose extends FragmentBase {
EntityAttachment attachment = new EntityAttachment(); EntityAttachment attachment = new EntityAttachment();
String name = uri.getLastPathSegment(); String fname = null;
String s = null; String ftype = null;
Long fsize = null;
try { try {
try (Cursor cursor = context.getContentResolver().query(uri, null, null, null, null, null)) { DocumentFile dfile = DocumentFile.fromSingleUri(context, uri);
if (cursor != null && cursor.moveToFirst()) { if (dfile != null) {
int colName = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); fname = dfile.getName();
int colSize = cursor.getColumnIndex(OpenableColumns.SIZE); ftype = dfile.getType();
if (colName >= 0) fsize = dfile.length();
name = cursor.getString(colName);
if (colSize >= 0)
s = cursor.getString(colSize);
}
} }
} catch (SecurityException ex) { } catch (SecurityException ex) {
Log.w(ex); Log.e(ex);
} }
if (TextUtils.isEmpty(fname))
fname = uri.getLastPathSegment();
if (TextUtils.isEmpty(ftype)) {
String extension = Helper.getExtension(fname);
if (extension != null)
ftype = MimeTypeMap.getSingleton()
.getMimeTypeFromExtension(extension.toLowerCase(Locale.ROOT));
if (ftype == null)
ftype = "application/octet-stream";
}
if (fsize != null && fsize <= 0)
fsize = null;
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
try { try {
db.beginTransaction(); db.beginTransaction();
@ -1995,17 +2008,10 @@ public class FragmentCompose extends FragmentBase {
attachment.message = draft.id; attachment.message = draft.id;
attachment.sequence = db.attachment().getAttachmentSequence(draft.id) + 1; attachment.sequence = db.attachment().getAttachmentSequence(draft.id) + 1;
attachment.name = name; attachment.name = fname;
attachment.type = ftype;
String extension = Helper.getExtension(attachment.name);
if (extension != null)
attachment.type = MimeTypeMap.getSingleton()
.getMimeTypeFromExtension(extension.toLowerCase(Locale.ROOT));
if (attachment.type == null)
attachment.type = "application/octet-stream";
attachment.disposition = (image ? Part.INLINE : Part.ATTACHMENT); attachment.disposition = (image ? Part.INLINE : Part.ATTACHMENT);
attachment.size = fsize;
attachment.size = (s == null ? null : Long.parseLong(s));
attachment.progress = 0; attachment.progress = 0;
attachment.id = db.attachment().insertAttachment(attachment); attachment.id = db.attachment().insertAttachment(attachment);