从URI获取文件

时间:2019-05-12 09:12:25

标签: android kotlin

我正在开发一项活动,用户可以选择图像或视频。选择图像或视频后,我将带有Retrofit的文件发送到asp.net核心rest api。我面临的问题是从URI获取文件。 onActivityResult方法的结果是一个内容路径,我需要File。当我尝试使用getPath()将内容路径转换为File时,总是得到null。

我尝试了其他一些选项,例如将图像转换为ByteArray。但是要将视频转换为ByteArray,我需要文件。

private fun uploadFile(fileUri: Uri, fieldId: Int) {
// Creating a RequestBody from the File.
val originalFile = File(getPath(fileUri))
val requestFile = RequestBody.create(
       MediaType.parse(this@CreateSuggestionFragment.context!!.contentResolver.getType(fileUri)!!),
        originalFile
    )

    val file: MultipartBody.Part

    // Create the File to send and the object for the REST-call.
    val suggestionField = currentSuggestionFields.find { sf -> sf.suggestionFieldId == fieldId }

    val fileType = if (suggestionField!!.fieldType == context!!.resources.getInteger(R.integer.SUG_PICTURE)) {
        "photo"
    } else {
        "video"
    }

    file = MultipartBody.Part.createFormData(fileType, originalFile.name, requestFile)

    // Add another part within the Multipart Request.
    val type = RequestBody.create(MultipartBody.FORM, fileType)

    // REST-call using Retrofit.
    val call = RetrofitFactory.getRetrofitFileApi().uploadFile(CurrentUser.getCurrentUser()!!.token, type, file)

编辑: 这是onActivityResult代码,我将Uri的代码保存在Map中。

/**
*This method will handle the startActivityForResult methods.
*/
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    // Result of Place Picker.
    if (placePickerRequestCodes!!.contains(requestCode) && resultCode == RESULT_OK) {
        // Retrieve the information from the selected location's CarmenFeature.
        val carmenFeature = PlacePicker.getPlace(data)
        if (carmenFeature != null) {
            buttons.find { b -> b.id == requestCode }!!.setBackgroundColor(
                this@CreateSuggestionFragment.resources.getColor(
                    R.color.currentObject
                )
            )

            val coordinates = carmenFeature.center()

            val latitude = coordinates!!.latitude()
            val longitude = coordinates.longitude()

            // Save the longitude and latitude.
            val selectedCoordinates = String.format("%s;%s", longitude, latitude)
            coordinateAnswers[requestCode] = selectedCoordinates
        }
    } else if (imagePickerRequestCodes!!.contains(requestCode) && resultCode == RESULT_OK && data != null && data.data != null) { // Result of Image Picker.
        val uri = data.data
        buttons.find { b -> b.id == requestCode }!!.setBackgroundColor(
            this@CreateSuggestionFragment.resources.getColor(
                R.color.currentObject
            )
        )

        fileUris.remove(requestCode)
        fileUris[requestCode] = uri
    } else if (videoPickerRequestCodes!!.contains(requestCode) && resultCode == RESULT_OK && data != null && data.data != null) { // Result of Video Picker.
        val uri = data.data
        buttons.find { b -> b.id == requestCode }!!.setBackgroundColor(
            this@CreateSuggestionFragment.resources.getColor(
                R.color.currentObject
            )
        )

        fileUris.remove(requestCode)
        fileUris[requestCode] = uri
    } else {
        Toast.makeText(
            this@CreateSuggestionFragment.context,
            "Resultaat niet goed ontvangen.",
            Toast.LENGTH_LONG
        )
            .show()
    }
}

这是我称为方法uploadFile的方法:

/**
 * This method will add EventHandlers to the necessary Views.
 */
private fun addEventHandlers() {
    btnSuggestionCreate.setOnClickListener {
        retrieveAnswers()

        if (controlInput()) {
            for ((k, v) in fileUris) {
                uploadFile(v, k)
            }

            submitSuggestion()
        } else {
            Toast.makeText(
                this@CreateSuggestionFragment.context,
                "Gelieve alle verplichte velden in te vullen.",
                Toast.LENGTH_LONG
            ).show()
        }
    }
}

我希望检索要发送的文件,但是在“ val originalFile = File(getPath(fileUri))”处收到空值

0 个答案:

没有答案