From f05a6ca0feb5d51b92ae5bf5a7a402a26dc416df Mon Sep 17 00:00:00 2001 From: sitou Date: Thu, 18 Sep 2025 09:26:18 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E6=B7=BB?= =?UTF-8?q?=E4=BB=98=E3=81=A7=E5=8B=95=E7=94=BB=E6=92=AE=E5=BD=B1=E3=81=A7?= =?UTF-8?q?=E3=81=8D=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/documentweb/DocumentWebDownload.kt | 46 ++++++++++++++++++- app/src/main/res/xml/file_path.xml | 1 + 2 files changed, 45 insertions(+), 2 deletions(-) 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 1db820a..16266db 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 ad3cd0d..8fe0180 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 -- GitLab