diff --git a/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebDownload.kt b/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebDownload.kt index 1db820a684b6a557547796b29e28a1517c026bd7..16266db2e9e6f1af4b7fb4a24e442a75843744ec 100644 --- a/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebDownload.kt +++ b/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebDownload.kt @@ -53,6 +53,7 @@ class DocumentWebDownloader(private val handlingFragment: Fragment, private val private var downloadSource: DownloadSource? = null private var currentPhotoPath: String? = null + private var currentVideoPath: String? = null private lateinit var requestPermissionLauncher: ActivityResultLauncher private var pendingIntent: Intent? = null @@ -121,9 +122,29 @@ class DocumentWebDownloader(private val handlingFragment: Fragment, private val takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI) } + // 動画撮影のIntentを作成 + val takeMovieIntent = Intent(MediaStore.ACTION_VIDEO_CAPTURE) + val videoFile: File? = try { + createVideoFile() // 動画用の一時ファイル作成 + } catch (ex: IOException) { + Toast.makeText(handlingFragment.requireContext(), "Failed to create video file.", Toast.LENGTH_SHORT).show() + null + } + videoFile?.also { + val videoURI: Uri = FileProvider.getUriForFile( + handlingFragment.requireContext(), + "jp.atled.agileworks.fileprovider", // AndroidManifest.xml で定義した authority + it + ) + takeMovieIntent.putExtra(MediaStore.EXTRA_OUTPUT, videoURI) + } + // 選択肢を提示する Intent を作成 val chooserIntent = Intent.createChooser(intent, "ファイルを選択") - chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, arrayOf(takePictureIntent)) + val intentArray = mutableListOf() + intentArray.add(takePictureIntent) + intentArray.add(takeMovieIntent) + chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray.toTypedArray()) handlingFragment.startActivityForResult(chooserIntent, chooseFileRequestCode) } @@ -152,6 +173,22 @@ class DocumentWebDownloader(private val handlingFragment: Fragment, private val } } + @Throws(IOException::class) + private fun createVideoFile(): File { + val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date()) + val storageDir: File? = handlingFragment.requireContext().getExternalFilesDir(Environment.DIRECTORY_MOVIES) + if (storageDir == null) { + throw IOException("Failed to get external storage directory for movies.") + } + return File.createTempFile( + "vid_${timeStamp}_", /* prefix */ + ".mp4", /* suffix */ + storageDir /* directory */ + ).apply { + currentVideoPath = absolutePath + } + } + /** * [showFileChooser] の結果を処理する。 * @@ -167,9 +204,13 @@ class DocumentWebDownloader(private val handlingFragment: Fragment, private val val result = WebChromeClient.FileChooserParams.parseResult(resultCode, data) filePathCallback?.onReceiveValue(result) } else if (currentPhotoPath != null) { - // カメラ撮影の結果 + // カメラ撮影(静止画)の結果 val uri = Uri.fromFile(File(currentPhotoPath!!)) filePathCallback?.onReceiveValue(arrayOf(uri)) + } else if (currentVideoPath != null) { + // カメラ撮影(動画)の結果 + val uri = Uri.fromFile(File(currentVideoPath!!)) + filePathCallback?.onReceiveValue(arrayOf(uri)) } else { filePathCallback?.onReceiveValue(null) } @@ -178,6 +219,7 @@ class DocumentWebDownloader(private val handlingFragment: Fragment, private val } filePathCallback = null currentPhotoPath = null + currentVideoPath = null } /** diff --git a/app/src/main/res/xml/file_path.xml b/app/src/main/res/xml/file_path.xml index ad3cd0d3728621a1804328b4172a37f2cce3b955..8fe01804e7414c167505ff7ac71e262dd6fd47ac 100644 --- a/app/src/main/res/xml/file_path.xml +++ b/app/src/main/res/xml/file_path.xml @@ -1,4 +1,5 @@ + \ No newline at end of file