Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MP4 videos saving result in 3GP on Android API <=28 #1188

Merged
merged 9 commits into from
Oct 30, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import java.io.File
import java.io.FileInputStream
import java.io.InputStream
import java.net.URLConnection
import android.text.TextUtils

@Suppress("Deprecation", "InlinedApi", "Range")
interface IDBUtils {
Expand Down Expand Up @@ -428,11 +429,6 @@ interface IDBUtils {
}
refreshStream()

val shouldKeepPath = if (!isAboveAndroidQ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also keep the condition? The fix should work without removing it, correct?

val dir = Environment.getExternalStorageDirectory()
file.absolutePath.startsWith(dir.path)
} else false

val timestamp = System.currentTimeMillis() / 1000
val values = ContentValues().apply {
put(
Expand All @@ -454,22 +450,23 @@ interface IDBUtils {
if (relativePath.isNotBlank()) {
put(RELATIVE_PATH, relativePath)
}
} else {
val albumDir = File(getAlbumFolderPath(title))
val videoFilePath = File(albumDir, file.name).absolutePath
put(DATA, videoFilePath)
}
if (latLong != null) {
put(MediaStore.Video.VideoColumns.LATITUDE, latLong.first())
put(MediaStore.Video.VideoColumns.LONGITUDE, latLong.last())
}
if (shouldKeepPath) {
put(DATA, filePath)
}
}

return insertUri(
context,
inputStream,
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
values,
shouldKeepPath
false
)?.copy(orientation = orientation ?: rotationDegrees)
}

Expand All @@ -493,6 +490,37 @@ interface IDBUtils {
return getAssetEntity(context, id.toString())
}

private fun getAlbumFolderPath(
folderName: String?,
): String {
var albumFolderPath: String = Environment.getExternalStorageDirectory().path
if (android.os.Build.VERSION.SDK_INT < 29) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What standard do we following here? Should we use MOVIE as much as possible?

albumFolderPath += File.separator + Environment.DIRECTORY_DCIM;
}
albumFolderPath = if (TextUtils.isEmpty(folderName)) {
var baseFolderName = Environment.DIRECTORY_MOVIES
createDirIfNotExist(
Environment.getExternalStoragePublicDirectory(baseFolderName).path
) ?: albumFolderPath
} else {
createDirIfNotExist(albumFolderPath + File.separator + folderName) ?: albumFolderPath
}
return albumFolderPath
}

private fun createDirIfNotExist(dirPath: String): String? {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We seem to already have a helper method checkDir

val dir = File(dirPath)
return if (!dir.exists()) {
if (dir.mkdirs()) {
dir.path
} else {
null
}
} else {
dir.path
}
}

fun assetExists(context: Context, id: String): Boolean {
val columns = arrayOf(_ID)
context.contentResolver.logQuery(allUri, columns, "$_ID = ?", arrayOf(id), null).use {
Expand Down
Loading