Android 调起文件,相册 实现选择图片与视频

	//调起所有文件 包括相册 视频 相机 录像
    private fun getAllFile() {
        //カメラの起動Intentの用意
        val photoName = System.currentTimeMillis().toString() + ".jpg"
        val contentValues = ContentValues()
        contentValues.put(MediaStore.Images.Media.TITLE, photoName)
        contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/*")
        mUri = context?.contentResolver?.insert(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            contentValues
        )

        val intentCamera = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, mUri)
        uiApplication!!.setCommonUri(mUri!!)

        var mUriVideo: Uri? = null
        val videoName = System.currentTimeMillis().toString() + ".mp4"
        val contentValues2 = ContentValues()
        contentValues2.put(MediaStore.Images.Media.TITLE, videoName)
        contentValues2.put(MediaStore.Images.Media.MIME_TYPE, "video/*")
        mUriVideo = context?.contentResolver?.insert(
            MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
            contentValues2
        )
        uiApplication!!.setCommonVideoUri(mUriVideo!!)
        val intentCameraVideo = Intent(MediaStore.ACTION_VIDEO_CAPTURE)
        intentCameraVideo.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0)
        intentCameraVideo.putExtra(MediaStore.EXTRA_OUTPUT, mUriVideo)

        // ギャラリー用のIntent作成
        val intentGallery = Intent(Intent.ACTION_GET_CONTENT)
        intentGallery.addCategory(Intent.CATEGORY_OPENABLE)
        intentGallery.type = "*/*"
        intentGallery.putExtra(Intent.EXTRA_MIME_TYPES, arrayOf("image/*", "video/*"))

        val intent = Intent.createChooser(intentCamera, "画像の選択")
        intent.putExtra(Intent.EXTRA_INITIAL_INTENTS, arrayOf(intentGallery, intentCameraVideo))
        mainActivity.startActivityForResult(intent, Constants.REQUEST_CODE_IMAGE_CHOOSER_REQUEST)
    }
——————————————————————————————————————————————————————————————————————————————————————
//选取照片文件和拍照
private fun getImageFile() {
        //カメラの起動Intentの用意
        val photoName = System.currentTimeMillis().toString() + ".jpg"
        val contentValues = ContentValues()
        contentValues.put(MediaStore.Images.Media.TITLE, photoName)
        contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/*")
        mUri = context?.contentResolver?.insert(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            contentValues
        )

        val intentCamera = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, mUri)
        uiApplication!!.setCommonUri(mUri!!)

        // ギャラリー用のIntent作成
        val intentGallery = Intent(Intent.ACTION_GET_CONTENT)
        intentGallery.addCategory(Intent.CATEGORY_OPENABLE)
        intentGallery.type = "*/*"
        intentGallery.putExtra(Intent.EXTRA_MIME_TYPES, arrayOf("image/*"))

        val intent = Intent.createChooser(intentCamera, "画像の選択")
        intent.putExtra(Intent.EXTRA_INITIAL_INTENTS, arrayOf(intentGallery))
        mainActivity.startActivityForResult(intent, Constants.REQUEST_CODE_IMAGE_CHOOSER_REQUEST)
    }
——————————————————————————————————————————————————————————————————————————————————————
//选择视频 照片回调
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK && requestCode == Constants.REQUEST_CODE_IMAGE_CHOOSER_REQUEST) {
            // カメラからの戻り
            var uri = if (data == null || data.data == null) uiApplication.getCommonUri()
                .toString() else data.data
            var videoUri = if (data == null || data.data == null) uiApplication.getCommonVideoUri()
                .toString() else data.data
            if ((data == null || data.data == null) && uriLength(applicationContext, Uri.parse(videoUri.toString())) > 0){
                uri = videoUri
            }
            if (uri.toString().contains("image")) {
                binding.apply {
                    onUpdateFileEvent(AddedAttachedFIlesEvent(data?.data, AttachedFileType.IMAGE))
                }

            } else if (uri.toString().contains("video")) {
                binding.apply {
                    onUpdateFileEvent(AddedAttachedFIlesEvent(data?.data, AttachedFileType.VIDEO))
                }
            } else {
                return;
            }
        }
        super.onActivityResult(requestCode, resultCode, data)
    }
——————————————————————————————————————————————————————————————————————————————————————
    private fun uriLength(context: Context, uri: Uri): Long =
        when (uri.scheme) {
            ContentResolver.SCHEME_FILE -> File(uri.path).length()
            ContentResolver.SCHEME_CONTENT -> try {
                context.contentResolver.openFileDescriptor(uri, "r")?.statSize ?: 0
            } catch (e: Exception) {
                0L
            }
            else -> 0L
        }

你可能感兴趣的:(Android)