Merge branch 'imageSaveFixes' into 'master'

Fix saving image with content link

See merge request pixeldroid/PixelDroid!453
This commit is contained in:
Matthieu 2022-07-27 20:31:31 +00:00
commit d700e92e78
3 changed files with 20 additions and 9 deletions

View File

@ -40,6 +40,7 @@ import org.pixeldroid.app.utils.db.entities.InstanceDatabaseEntity
import org.pixeldroid.app.utils.db.entities.UserDatabaseEntity
import org.pixeldroid.app.utils.ffmpegSafeUri
import org.pixeldroid.app.utils.fileExtension
import org.pixeldroid.app.utils.getMimeType
import java.io.File
import java.io.OutputStream
import java.text.SimpleDateFormat
@ -247,7 +248,7 @@ class PostCreationActivity : BaseThemedWithoutBarActivity() {
val path: String
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val resolver: ContentResolver = contentResolver
val type = resolver.getType(uri)
val type = uri.getMimeType(contentResolver)
val contentValues = ContentValues()
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name)
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, type)
@ -255,10 +256,10 @@ class PostCreationActivity : BaseThemedWithoutBarActivity() {
MediaStore.MediaColumns.RELATIVE_PATH,
Environment.DIRECTORY_PICTURES
)
val imageUri: Uri = resolver.insert(
if (type?.startsWith("image") == true) MediaStore.Images.Media.EXTERNAL_CONTENT_URI
else MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
contentValues)!!
val store =
if (type.startsWith("video")) MediaStore.Video.Media.EXTERNAL_CONTENT_URI
else MediaStore.Images.Media.EXTERNAL_CONTENT_URI
val imageUri: Uri = resolver.insert(store, contentValues)!!
path = imageUri.toString()
outputStream = resolver.openOutputStream(Objects.requireNonNull(imageUri))!!
} else {

View File

@ -28,6 +28,7 @@ import org.pixeldroid.app.utils.PixelDroidApplication
import org.pixeldroid.app.utils.api.objects.Attachment
import org.pixeldroid.app.utils.db.entities.InstanceDatabaseEntity
import org.pixeldroid.app.utils.di.PixelfedAPIHolder
import org.pixeldroid.app.utils.getMimeType
import retrofit2.HttpException
import java.io.File
import java.io.FileNotFoundException
@ -159,8 +160,8 @@ class PostCreationViewModel(application: Application, clipdata: ClipData? = null
}
val sizeInkBytes = ceil(size.toDouble() / 1000).toLong()
val type = getApplication<PixelDroidApplication>().contentResolver.getType(uri)
val isVideo = type?.startsWith("video/") == true
val type = uri.getMimeType(getApplication<PixelDroidApplication>().contentResolver)
val isVideo = type.startsWith("video/")
if(isVideo && !instance!!.videoEnabled){
_uiState.update { currentUiState ->

View File

@ -11,8 +11,8 @@ import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import android.util.DisplayMetrics
import android.util.TypedValue
import android.view.WindowManager
import android.webkit.MimeTypeMap
import androidx.annotation.AttrRes
import androidx.annotation.ColorInt
import androidx.annotation.StyleRes
@ -56,10 +56,19 @@ fun Uri.fileExtension(contentResolver: ContentResolver): String? {
return if (scheme == "content") {
contentResolver.getType(this)?.takeLastWhile { it != '/' }
} else {
toString().takeLastWhile { it != '/' }
MimeTypeMap.getFileExtensionFromUrl(toString()).ifEmpty { null }
}
}
fun Uri.getMimeType(contentResolver: ContentResolver, fallback: String = "image/*"): String {
return if (scheme == "content") {
contentResolver.getType(this)
} else {
MimeTypeMap.getFileExtensionFromUrl(toString())
?.run { MimeTypeMap.getSingleton().getMimeTypeFromExtension(toLowerCase()) }
} ?: fallback
}
fun Context.displayDimensionsInPx(): Pair<Int, Int> {
val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager