add file compression
This commit is contained in:
parent
43132f9cdf
commit
209266116b
@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
)
|
||||
|
@ -13,7 +13,11 @@ class SettingsViewModel : ViewModel() {
|
||||
|
||||
//size of the file, grab this for use
|
||||
private val _size = MutableLiveData<Double>().apply {
|
||||
value = 8.0
|
||||
value = 25.0
|
||||
}
|
||||
val size: MutableLiveData<Double> = _size
|
||||
|
||||
fun getSize(): Double{
|
||||
return size.value!!
|
||||
}
|
||||
}
|
@ -5,7 +5,7 @@
|
||||
<string name="title_settings">Settings</string>
|
||||
<string name="settings_video_size">Video Size</string>
|
||||
<string name="settings_image_size">Image Size</string>
|
||||
<string name="settings_default_size">8mb</string>
|
||||
<string name="settings_default_size">25mb</string>
|
||||
<string name="settings_big_size">50mb</string>
|
||||
<string name="settings_huge_size">500mb</string>
|
||||
<string name="settings_custom_size">Custom</string>
|
||||
|
Loading…
Reference in New Issue
Block a user