From 6fc62d3c0141ef35316cacc17c3a2fbd0524d3a3 Mon Sep 17 00:00:00 2001 From: lenchan139 Date: Thu, 30 May 2019 02:26:33 +0800 Subject: [PATCH] make a copy of file before upload (#1294) --- .../keylesspalace/tusky/ComposeActivity.java | 39 +++++++++++++++++-- app/src/main/res/xml/file_paths.xml | 1 + 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java index fbdfdfe1..720b221e 100644 --- a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java @@ -85,6 +85,7 @@ import com.keylesspalace.tusky.service.SendTootService; import com.keylesspalace.tusky.util.ComposeTokenizer; import com.keylesspalace.tusky.util.CountUpDownLatch; import com.keylesspalace.tusky.util.DownsizeImageTask; +import com.keylesspalace.tusky.util.IOUtils; import com.keylesspalace.tusky.util.ImageLoadingHelper; import com.keylesspalace.tusky.util.ListUtils; import com.keylesspalace.tusky.util.SaveTootHelper; @@ -103,6 +104,7 @@ import org.jetbrains.annotations.NotNull; import java.io.File; import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -1513,13 +1515,44 @@ public final class ComposeActivity } - private void pickMedia(Uri uri, long mediaSize, String description) { + private void pickMedia(Uri inUri, long mediaSize, String description) { + Uri uri = inUri; + ContentResolver contentResolver = getContentResolver(); + String mimeType = contentResolver.getType(uri); + + InputStream tempInput = null; + FileOutputStream out = null; + String filename = inUri.toString().substring(inUri.toString().lastIndexOf("/")); + int suffixPosition = filename.lastIndexOf("."); + String suffix = ""; + if(suffixPosition > 0) suffix = filename.substring(suffixPosition); + try { + tempInput = getContentResolver().openInputStream(inUri); + File file = File.createTempFile("randomTemp1", suffix, getCacheDir()); + out = new FileOutputStream(file.getAbsoluteFile()); + byte[] buff = new byte[1024]; + int read = 0; + while ((read = tempInput.read(buff)) > 0) { + out.write(buff, 0, read); + } + uri = FileProvider.getUriForFile(this, + BuildConfig.APPLICATION_ID+".fileprovider", + file); + mediaSize = getMediaSize(getContentResolver(), uri); + tempInput.close(); + out.close(); + } catch(IOException e) { + Log.w(TAG, e); + uri = inUri; + } finally { + IOUtils.closeQuietly(tempInput); + IOUtils.closeQuietly(out); + } + if (mediaSize == MEDIA_SIZE_UNKNOWN) { displayTransientError(R.string.error_media_upload_opening); return; } - ContentResolver contentResolver = getContentResolver(); - String mimeType = contentResolver.getType(uri); if (mimeType != null) { String topLevelType = mimeType.substring(0, mimeType.indexOf('/')); switch (topLevelType) { diff --git a/app/src/main/res/xml/file_paths.xml b/app/src/main/res/xml/file_paths.xml index 355bae57..61f9cdef 100644 --- a/app/src/main/res/xml/file_paths.xml +++ b/app/src/main/res/xml/file_paths.xml @@ -1,4 +1,5 @@ + \ No newline at end of file