From 6a460b2952b92505813f2bb8632592795be7a8a9 Mon Sep 17 00:00:00 2001 From: lionel <> Date: Fri, 25 Mar 2022 16:26:37 +0100 Subject: [PATCH] Fix upload mms again --- .../java/com/localtransfer/UploadFile.java | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/localtransfer/UploadFile.java b/app/src/main/java/com/localtransfer/UploadFile.java index 84fc0f4..bc194b9 100644 --- a/app/src/main/java/com/localtransfer/UploadFile.java +++ b/app/src/main/java/com/localtransfer/UploadFile.java @@ -28,6 +28,8 @@ public class UploadFile extends Transfer { public Uri uri; + public String type; + public UploadFile() { this.id = View.generateViewId(); instances.add(this); @@ -44,9 +46,19 @@ public class UploadFile extends Transfer { Cursor cursor = activity.getContentResolver().query(uri, null, null, null, null); cursor.moveToFirst(); - file.name = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); - file.size = cursor.getLong(cursor.getColumnIndex(OpenableColumns.SIZE)); - file.sizeSI = Transfer.humanReadableByteCountBin(file.size); + file.type = activity.getContentResolver().getType(uri); + + int col_name = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); + if (col_name != -1) + file.name = cursor.getString(col_name); + if(file.name == null) + file.name = file.type.split("/")[0] + "_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + "." + file.type.split("/")[1]; + + int col_size = cursor.getColumnIndex(OpenableColumns.SIZE); + if (col_name != -1) { + file.size = cursor.getLong(col_size); + file.sizeSI = Transfer.humanReadableByteCountBin(file.size); + } file.AddTransfer(); } @@ -116,15 +128,7 @@ public class UploadFile extends Transfer { Cursor cursor = activity.getContentResolver().query(uri, null, null, null, null); cursor.moveToFirst(); - String type = activity.getContentResolver().getType(uri); - - String fileName = null; - int col_name = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); - if (col_name != -1) - fileName = cursor.getString(col_name); - if(fileName == null) - fileName = type.split("/")[0] + "_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + "." + type.split("/")[1]; - String fileNameUTF8 = new String(fileName.getBytes(), StandardCharsets.ISO_8859_1); + String fileNameUTF8 = new String(name.getBytes(), StandardCharsets.ISO_8859_1); url = new URL(protocol, host, port, root + "/upload.php"); @@ -152,6 +156,7 @@ public class UploadFile extends Transfer { InputStream in = activity.getContentResolver().openInputStream(uri); size = in.available(); + sizeSI = Transfer.humanReadableByteCountBin(size); while ((bufferLength = in.read(buffer)) > 0) { request.write(buffer, 0, bufferLength); @@ -168,7 +173,7 @@ public class UploadFile extends Transfer { int responseCode = conn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { - message = "File " + fileName + " successful upload"; + message = "File " + name + " successful upload"; state = Transfer.STATE_SUCCESS; progressEnd(); }