Fix upload MMS without file name and size

This commit is contained in:
lionel 2022-02-07 23:41:03 +01:00
parent cba7fc2555
commit 057038cca7

View File

@ -7,19 +7,14 @@ import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.OpenableColumns;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.FileProvider;
import androidx.preference.PreferenceManager;
@ -32,7 +27,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
@ -41,9 +35,7 @@ import java.nio.charset.StandardCharsets;
import java.text.CharacterIterator;
import java.text.SimpleDateFormat;
import java.text.StringCharacterIterator;
import java.util.ConcurrentModificationException;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
@ -141,7 +133,7 @@ public class Transfer {
public void handleSendText(String sharedText) {
if (sharedText != null) {
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyyMMdd_HHmmss");
SimpleDateFormat sdfDate = new SimpleDateFormat("text_yyyyMMdd_HHmmss");
Date now = new Date();
String strDate = sdfDate.format(now);
new Thread(() -> {
@ -171,26 +163,33 @@ public class Transfer {
HttpURLConnection conn = null;
DataOutputStream request = null;
String boundary = "*****";
int maxBufferSize = 1 * 1024 * 1024;
final int maxBufferSize = 1 * 1024 * 1024;
byte[] buffer = new byte[maxBufferSize];
int bufferLength;
long loaded = 0;
Cursor cursor = activity.getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
String fileName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
String fileNameUTF8 = new String(fileName.getBytes(), StandardCharsets.ISO_8859_1);
long fileSize = cursor.getLong(cursor.getColumnIndex(OpenableColumns.SIZE));
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);
long fileSize = -1;
int col_size = cursor.getColumnIndex(OpenableColumns.SIZE);
if (col_size != -1)
fileSize = cursor.getLong(col_size);
url = new URL(protocol, host, port, root + "/upload.php");
url = checkRedirection(url);
Progress p = new Progress(fileName, fileSize, Progress.UPLOAD);
if(url != null) {
Log.d("URL", url.toString());
conn = (HttpURLConnection) url.openConnection();
@ -212,6 +211,11 @@ public class Transfer {
InputStream in = activity.getContentResolver().openInputStream(uri);
if (fileSize < 1)
fileSize = in.available();
Progress p = new Progress(fileName, fileSize, Progress.UPLOAD);
while ((bufferLength = in.read(buffer)) > 0) {
request.write(buffer, 0, bufferLength);
loaded+= (long) bufferLength;
@ -248,7 +252,6 @@ public class Transfer {
Snackbar.make(activity.findViewById(R.id.view_pager), finalMessage, Snackbar.LENGTH_LONG)
.setAction("Action", null).show());
}
}
return message;
}