Check file size before compression
This commit is contained in:
parent
07cd3372d8
commit
74c5e4384c
2 changed files with 55 additions and 11 deletions
|
|
@ -5,6 +5,7 @@ import android.app.Notification
|
|||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.ContentResolver
|
||||
import android.content.ContentValues
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
|
|
@ -17,6 +18,7 @@ import android.net.Uri
|
|||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.provider.ContactsContract.PhoneLookup
|
||||
import android.provider.OpenableColumns
|
||||
import android.provider.Telephony.*
|
||||
import android.text.TextUtils
|
||||
import androidx.core.app.NotificationCompat
|
||||
|
|
@ -35,6 +37,7 @@ import com.simplemobiletools.smsmessenger.interfaces.MessagesDao
|
|||
import com.simplemobiletools.smsmessenger.models.*
|
||||
import com.simplemobiletools.smsmessenger.receivers.DirectReplyReceiver
|
||||
import com.simplemobiletools.smsmessenger.receivers.MarkAsReadReceiver
|
||||
import java.io.FileNotFoundException
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
import me.leolin.shortcutbadger.ShortcutBadger
|
||||
|
|
@ -788,3 +791,38 @@ fun Context.updateLastConversationMessage(threadId: Long) {
|
|||
} catch (e: Exception) {
|
||||
}
|
||||
}
|
||||
|
||||
fun Context.getFileSizeFromUri(uri:Uri): Long {
|
||||
val assetFileDescriptor = try {
|
||||
contentResolver.openAssetFileDescriptor(uri, "r")
|
||||
} catch (e: FileNotFoundException) {
|
||||
null
|
||||
}
|
||||
|
||||
// uses ParcelFileDescriptor#getStatSize underneath if failed
|
||||
val length = assetFileDescriptor?.use { it.length } ?: FILE_SIZE_NONE
|
||||
if (length != -1L) {
|
||||
return length
|
||||
}
|
||||
|
||||
// if "content://" uri scheme, try contentResolver table
|
||||
if (uri.scheme.equals(ContentResolver.SCHEME_CONTENT)) {
|
||||
return contentResolver.query(uri, arrayOf(OpenableColumns.SIZE), null, null, null)
|
||||
?.use { cursor ->
|
||||
// maybe shouldn't trust ContentResolver for size:
|
||||
// https://stackoverflow.com/questions/48302972/content-resolver-returns-wrong-size
|
||||
val sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE)
|
||||
if (sizeIndex == -1) {
|
||||
return@use FILE_SIZE_NONE
|
||||
}
|
||||
cursor.moveToFirst()
|
||||
return try {
|
||||
cursor.getLong(sizeIndex)
|
||||
} catch (_: Throwable) {
|
||||
FILE_SIZE_NONE
|
||||
}
|
||||
} ?: FILE_SIZE_NONE
|
||||
} else {
|
||||
return FILE_SIZE_NONE
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue