From 209266116bbbc947727e3a59100acfa7ad40f877 Mon Sep 17 00:00:00 2001 From: Ethan Pearson <60271460+magmajp@users.noreply.github.com> Date: Tue, 6 Dec 2022 22:05:28 -0400 Subject: [PATCH] add file compression --- .../com/example/myapplication/MainActivity.kt | 69 ++++++++++++------- .../ui/settings/SettingsFragment.kt | 23 ++++--- .../ui/settings/SettingsViewModel.kt | 6 +- app/src/main/res/values/strings.xml | 2 +- 4 files changed, 64 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/com/example/myapplication/MainActivity.kt b/app/src/main/java/com/example/myapplication/MainActivity.kt index c1d5577..7decd15 100644 --- a/app/src/main/java/com/example/myapplication/MainActivity.kt +++ b/app/src/main/java/com/example/myapplication/MainActivity.kt @@ -4,7 +4,9 @@ import android.Manifest import android.annotation.SuppressLint import android.content.Intent import android.content.pm.PackageManager +import android.media.MediaMetadataRetriever import android.net.Uri +import android.os.Build import android.os.Bundle import android.os.Handler import android.os.Looper @@ -14,6 +16,7 @@ import android.view.Menu import android.view.MenuItem import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts +import androidx.activity.viewModels import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.navigation.findNavController @@ -25,6 +28,8 @@ import com.arthenica.ffmpegkit.FFmpegKitConfig import com.example.myapplication.databinding.ActivityMainBinding import com.example.myapplication.ui.compressing.CompressingAdapter import com.example.myapplication.ui.compressing.CompressingItem +import com.example.myapplication.ui.settings.SettingsFragment +import com.example.myapplication.ui.settings.SettingsViewModel import com.google.android.material.bottomnavigation.BottomNavigationView import java.io.File import java.util.* @@ -33,6 +38,7 @@ class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding + private val settingsViewModel: SettingsViewModel by viewModels() override fun onCreate(savedInstanceState: Bundle?) { @@ -79,7 +85,7 @@ class MainActivity : AppCompatActivity() { //runs when pressing "Files" R.id.addFile -> { val intent = Intent() - .setType("*/*") + .setType("video/*") .setAction(Intent.ACTION_GET_CONTENT) .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) .addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION) @@ -87,7 +93,7 @@ class MainActivity : AppCompatActivity() { resultLauncher.launch(intent) - Toast.makeText(applicationContext, "Files", Toast.LENGTH_LONG).show() + //Toast.makeText(applicationContext, "Files", Toast.LENGTH_LONG).show() return true } @@ -114,37 +120,52 @@ class MainActivity : AppCompatActivity() { cursor?.moveToFirst() val fileDate = Date(System.currentTimeMillis()) - val fileName = cursor?.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME)) + val fileName = + cursor?.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME)) + val fileSize = + cursor?.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE)) + ?.toDouble() - val item = CompressingItem(fileName!!, 0.0, fileDate) + if (fileSize!! / 1000000 > settingsViewModel.getSize()) { - val outputFile = File(this.getExternalFilesDir(null), "converted_$fileName") + val mmr = MediaMetadataRetriever() + mmr.setDataSource(this, data) + val duration = + mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)?.toDouble() + Log.i("ethan", duration.toString()) - compressingItems.add(item) + Log.i("ethan", settingsViewModel.getSize().toString()) + Log.i("ethan", fileSize.toString()) + val bitrate = (settingsViewModel.getSize()*1000000) / (duration!!/1000) - val handler = Handler(Looper.getMainLooper()) + val item = CompressingItem(fileName!!, 0.0, fileDate) + val outputFile = File(this.getExternalFilesDir(null), "converted_$fileName") - val command = "-i $inUri -c:v mpeg4 ${outputFile.absolutePath} -y" - val session = FFmpegKit.executeAsync(command) { - compressingItems.remove(item) + compressingItems.add(item) - handler.post { - Toast.makeText(this, "Finished converting $fileName", Toast.LENGTH_SHORT).show() - adapter.notifyDataSetChanged() + val handler = Handler(Looper.getMainLooper()) + + val command = "-i $inUri -b:v $bitrate ${outputFile.absolutePath} -y" + Log.i("ethan",command) + val session = FFmpegKit.executeAsync(command) { + compressingItems.remove(item) + + handler.post { + Toast.makeText(this, "Finished converting $fileName", Toast.LENGTH_SHORT).show() + adapter.notifyDataSetChanged() + } } + + + Log.i("Tag", Arrays.deepToString(session.arguments)) + Log.i("Tag", session.output) + + + adapter.notifyDataSetChanged() + }else{ + Toast.makeText(applicationContext,"File is less than target size",Toast.LENGTH_LONG).show() } - - - Log.i("Tag", Arrays.deepToString(session.arguments)) - Log.i("Tag", session.output) - - - - - - - adapter.notifyDataSetChanged() } } diff --git a/app/src/main/java/com/example/myapplication/ui/settings/SettingsFragment.kt b/app/src/main/java/com/example/myapplication/ui/settings/SettingsFragment.kt index 5e2b11b..b5c3eb7 100644 --- a/app/src/main/java/com/example/myapplication/ui/settings/SettingsFragment.kt +++ b/app/src/main/java/com/example/myapplication/ui/settings/SettingsFragment.kt @@ -7,7 +7,9 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Toast +import androidx.activity.viewModels import androidx.fragment.app.Fragment +import androidx.fragment.app.activityViewModels import androidx.lifecycle.ViewModelProvider import com.example.myapplication.databinding.FragmentSettingsBinding @@ -20,12 +22,13 @@ class SettingsFragment : Fragment() { // onDestroyView. private val binding get() = _binding!! + private val settingsViewModel: SettingsViewModel by activityViewModels() + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { - val homeViewModel = ViewModelProvider(this)[SettingsViewModel::class.java] _binding = FragmentSettingsBinding.inflate(inflater, container, false) val root: View = binding.root @@ -41,20 +44,20 @@ class SettingsFragment : Fragment() { binding.settingRadioGroup.setOnCheckedChangeListener{ group,checkedID -> if(checkedID == binding.settingDefaultSizeVideo.id){ - homeViewModel.size.value = 8.0 + settingsViewModel.size.value = 25.0 }else if(checkedID == binding.settingBigSizeVideo.id){ - homeViewModel.size.value = 50.0 + settingsViewModel.size.value = 50.0 }else if(checkedID == binding.settingHugeSize.id){ - homeViewModel.size.value = 500.0 + settingsViewModel.size.value = 500.0 }else{ try{ - homeViewModel.size.value = binding.settingCustomSizeVideoText.text.toString().toDouble() + settingsViewModel.size.value = binding.settingCustomSizeVideoText.text.toString().toDouble() }catch (e: NumberFormatException){ - homeViewModel.size.value = 0.0 + settingsViewModel.size.value = 0.0 } } - Toast.makeText(root.context,"" + homeViewModel.size.value,Toast.LENGTH_SHORT).show() + Toast.makeText(root.context,"" + settingsViewModel.size.value,Toast.LENGTH_SHORT).show() } @@ -70,11 +73,11 @@ class SettingsFragment : Fragment() { override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { try{ - homeViewModel.size.value = s.toString().toDouble() + settingsViewModel.size.value = s.toString().toDouble() }catch (e: NumberFormatException){ - homeViewModel.size.value = 0.0 + settingsViewModel.size.value = 0.0 } - Toast.makeText(root.context,"" + homeViewModel.size.value,Toast.LENGTH_SHORT).show() + Toast.makeText(root.context,"" + settingsViewModel.size.value,Toast.LENGTH_SHORT).show() } } ) diff --git a/app/src/main/java/com/example/myapplication/ui/settings/SettingsViewModel.kt b/app/src/main/java/com/example/myapplication/ui/settings/SettingsViewModel.kt index f681f6c..dd30e1e 100644 --- a/app/src/main/java/com/example/myapplication/ui/settings/SettingsViewModel.kt +++ b/app/src/main/java/com/example/myapplication/ui/settings/SettingsViewModel.kt @@ -13,7 +13,11 @@ class SettingsViewModel : ViewModel() { //size of the file, grab this for use private val _size = MutableLiveData().apply { - value = 8.0 + value = 25.0 } val size: MutableLiveData = _size + + fun getSize(): Double{ + return size.value!! + } } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 98c3fb5..2576b46 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -5,7 +5,7 @@ Settings Video Size Image Size - 8mb + 25mb 50mb 500mb Custom