MainActivity: prevent crash on file too big

Also, new (and last) sound.
This commit is contained in:
dece 2021-03-28 00:16:01 +01:00
parent ed91e85e31
commit 0e2d4a8f12
4 changed files with 18 additions and 10 deletions

View file

@ -5,7 +5,6 @@ import android.app.Activity
import android.app.AlertDialog import android.app.AlertDialog
import android.content.ContentValues import android.content.ContentValues
import android.content.Context import android.content.Context
import android.content.DialogInterface
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.net.Uri import android.net.Uri
@ -153,6 +152,7 @@ class MainActivity : AppCompatActivity() {
private fun processVideoUri(inputVideoUri: Uri) { private fun processVideoUri(inputVideoUri: Uri) {
// Save captured video for FFMPEG. Use MP4 extension, completely arbitrary. // Save captured video for FFMPEG. Use MP4 extension, completely arbitrary.
val inputFile = File.createTempFile("input", ".mp4", cacheDir) val inputFile = File.createTempFile("input", ".mp4", cacheDir)
try {
contentResolver.openInputStream(inputVideoUri).use { inputStream -> contentResolver.openInputStream(inputVideoUri).use { inputStream ->
if (inputStream == null) if (inputStream == null)
return Unit.also { Log.e(TAG, "Could not open input file") } return Unit.also { Log.e(TAG, "Could not open input file") }
@ -160,6 +160,10 @@ class MainActivity : AppCompatActivity() {
fos.buffered().write(inputStream.buffered().readBytes()) fos.buffered().write(inputStream.buffered().readBytes())
} }
} }
} catch (e: OutOfMemoryError) {
toast("Video is too large, Android broke!")
return
}
processVideo(inputFile.canonicalPath) processVideo(inputFile.canonicalPath)
} }
@ -179,14 +183,14 @@ class MainActivity : AppCompatActivity() {
true true
} }
} ?: true } ?: true
val duration = mediaInfo?.duration ?: 0 val duration = mediaInfo?.duration?.toLong() ?: 0L
// Save the soundtrack as well cause we can't pass a resource to FFMPEG. // Save the soundtrack as well cause we can't pass a resource to FFMPEG.
// Should be done only once, then it's kept in app internal storage. // Should be done only once, then it's kept in app internal storage.
val themeId = getThemeRawResource(getThemeId()) val themeId = getThemeId()
val soundFile = File(filesDir, "$themeId.m4a") val soundFile = File(filesDir, "$themeId.m4a")
if (!soundFile.exists()) { if (!soundFile.exists()) {
resources.openRawResource(themeId).use { resource -> resources.openRawResource(getThemeRawResource(themeId)).use { resource ->
FileOutputStream(soundFile).use { fos -> FileOutputStream(soundFile).use { fos ->
fos.buffered().write(resource.buffered().readBytes()) fos.buffered().write(resource.buffered().readBytes())
} }
@ -288,10 +292,11 @@ class MainActivity : AppCompatActivity() {
} }
} }
@Suppress("DEPRECATION")
private fun save(): Boolean { private fun save(): Boolean {
if (currentVideoPath == null) if (currentVideoPath == null)
return false return false
val currentFile = File(currentVideoPath) val currentFile = File(currentVideoPath!!)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val store = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY) val store = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
val videoDetails = ContentValues() val videoDetails = ContentValues()
@ -358,6 +363,7 @@ class MainActivity : AppCompatActivity() {
when (id) { when (id) {
0 -> R.raw.harvestdawn 0 -> R.raw.harvestdawn
1 -> R.raw.sunriseofflutes 1 -> R.raw.sunriseofflutes
2 -> R.raw.deathknell
else -> R.raw.harvestdawn else -> R.raw.harvestdawn
} }

Binary file not shown.

View file

@ -3,5 +3,6 @@
<string-array name="pick_theme"> <string-array name="pick_theme">
<item>@string/theme_harvestdawn</item> <item>@string/theme_harvestdawn</item>
<item>@string/theme_sunriseofflutes</item> <item>@string/theme_sunriseofflutes</item>
<item>@string/theme_deathknell</item>
</string-array> </string-array>
</resources> </resources>

View file

@ -10,4 +10,5 @@
<string name="action_theme">Theme</string> <string name="action_theme">Theme</string>
<string name="theme_harvestdawn">Harvest Dawn</string> <string name="theme_harvestdawn">Harvest Dawn</string>
<string name="theme_sunriseofflutes">Sunrise of Flutes</string> <string name="theme_sunriseofflutes">Sunrise of Flutes</string>
<string name="theme_deathknell">Death Knell</string>
</resources> </resources>