瀏覽代碼

Share to youtube ok

Wayne 6 年之前
父節點
當前提交
25636dec34

+ 65 - 17
src/main/java/com/bomostory/sceneeditmodule/screen/movie/MovieEditActivity.kt

@@ -4,13 +4,17 @@ import android.app.AlertDialog
 import android.app.ProgressDialog
 import android.arch.lifecycle.Observer
 import android.arch.lifecycle.ViewModelProviders
+import android.content.ContentResolver
+import android.content.ContentValues
 import android.content.DialogInterface
 import android.content.Intent
 import android.net.Uri
 import android.os.Bundle
 import android.os.Environment
 import android.os.Handler
+import android.provider.MediaStore
 import android.support.v4.app.DialogFragment
+import android.support.v4.content.FileProvider
 import android.support.v7.app.AppCompatActivity
 import android.view.View
 import android.widget.CompoundButton
@@ -375,7 +379,37 @@ class MovieEditActivity : AppCompatActivity(),
                 override fun onStopTrackingTouch(seekBar: SeekBar?) {
                 }
             }
-            onClickBomo = Runnable { saveMovie() }
+            onClickBomo = Runnable {
+                saveMovie(object : OnMovieSavedListener {
+                    override fun onSaved(file: File) {
+                    AlertDialog.Builder(this@MovieEditActivity)
+                            .setMessage("Complete")
+                            .setPositiveButton("Play") { _, _ ->
+                                val uri = Uri.parse(file.absolutePath)
+                                val intent = Intent(Intent.ACTION_VIEW, uri).apply {
+                                    setDataAndType(uri, "video/mp4")
+                                }
+                                if (intent.resolveActivity(packageManager) != null) {
+                                    Intent.createChooser(intent, "").apply {
+                                        startActivity(this)
+                                    }
+                                } else {
+                                    Toast.makeText(this@MovieEditActivity, "No app can play video", Toast.LENGTH_SHORT).show()
+                                }
+                            }.show()
+                    }
+                })
+            }
+            onClickYoutube = Runnable {
+                saveMovie(object : OnMovieSavedListener {
+                    override fun onSaved(file: File) {
+                        val isSuc = shareToOtherApp(file, "com.google.android.youtube")
+                        if (!isSuc) {
+                            Toast.makeText(context, R.string.share_youtube_not_found, Toast.LENGTH_SHORT).show()
+                        }
+                    }
+                })
+            }
             onClickExportPdf = Runnable { onClickExportPdf() }
             onClickPlay = Runnable {
                 moviePlayer.moviePlayListener = dialogMoviePlayListener
@@ -385,6 +419,30 @@ class MovieEditActivity : AppCompatActivity(),
         }.show(supportFragmentManager)
     }
 
+    private fun shareToOtherApp(file: File, packageNameKeyWord: String): Boolean {
+        val authority = "$packageName"
+        val context = this@MovieEditActivity
+        val uri = FileProvider.getUriForFile(context, authority, file)
+
+        val intent = Intent(Intent.ACTION_SEND)
+        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET)
+        intent.type = "video/mp4"
+        intent.putExtra(Intent.EXTRA_STREAM, uri)
+
+        val activities = packageManager.queryIntentActivities(intent, 0)
+        if (!activities.isEmpty()) {
+            activities.forEach {
+                val name = it.activityInfo.packageName
+                if (name.contains(packageNameKeyWord)) {
+                    intent.setClassName(name, it.activityInfo.name)
+                    startActivity(intent)
+                    return true
+                }
+            }
+        }
+        return false
+    }
+
     private fun onClickExportPdf() {
         ExportPdfDialog().also { exportPdfDialog ->
             exportPdfDialog.image1Path = viewModel.project?.scene1File?.path ?: ""
@@ -422,7 +480,11 @@ class MovieEditActivity : AppCompatActivity(),
         }.show(supportFragmentManager)
     }
 
-    private fun saveMovie() {
+    interface OnMovieSavedListener {
+        fun onSaved(file: File)
+    }
+
+    private fun saveMovie(onMovieSavedListener: OnMovieSavedListener) {
         val scaledWidth = resources.getDimensionPixelSize(R.dimen.movie_width)
         val scaleHeight = resources.getDimensionPixelSize(R.dimen.movie_height)
 
@@ -462,21 +524,7 @@ class MovieEditActivity : AppCompatActivity(),
                     AlertDialog.Builder(this).setMessage(msg).show()
                     delayedHide(100)
                 }, {
-                    AlertDialog.Builder(this)
-                            .setMessage("Complete")
-                            .setPositiveButton("Play") { _, _ ->
-                                val uri = Uri.parse(outputFile.absolutePath)
-                                val intent = Intent(Intent.ACTION_VIEW, uri).apply {
-                                    setDataAndType(uri, "video/mp4")
-                                }
-                                if (intent.resolveActivity(packageManager) != null) {
-                                    Intent.createChooser(intent, "").apply {
-                                        startActivity(this)
-                                    }
-                                } else {
-                                    Toast.makeText(this, "No app can play video", Toast.LENGTH_SHORT).show()
-                                }
-                            }.show()
+                    onMovieSavedListener.onSaved(outputFile)
                     delayedHide(100)
                 })
     }

+ 2 - 0
src/main/res/values/strings.xml

@@ -22,4 +22,6 @@
     <string name="export_pdf_dialog_radio_foldable_booklet">Foldable Booklet</string>
     <string name="export_pdf_dialog_print">Print</string>
     <string name="export_pdf_dialog_cancel">Cancel</string>
+
+    <string name="share_youtube_not_found">Cannot find Youtube.</string>
 </resources>