how to get file(pdf, jpg, docs) from local storage and create File [duplicate]

ぃ、小莉子 提交于 2020-01-22 03:30:10

问题


I tried to create file with intent data uri

To create File, I start intent like this

val intent = Intent(Intent.ACTION_GET_CONTENT).apply {
                addCategory(Intent.CATEGORY_OPENABLE)
                type = "*/*"
            }
            startActivityForResult(intent, PICK_FROM_LOCAL)

And this is onActivityResult code

super.onActivityResult(requestCode, resultCode, data)
    if (requestCode === PICK_FROM_LOCAL && resultCode === Activity.RESULT_OK) {
        val uri = data?.data

        val uriPath = uri?.path
        val file = File(uriPath)
        Log.i("File Path", file.path)
        if (file.exists()) {
            viewModel.hasFile = true
            val requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file)
            val multipartData =
                MultipartBody.Part.createFormData("file", file.name, requestFile)
            viewModel.file = multipartData
        }
    }

This is Log -> I/File Path: /document/image:5187

file.exists() returns false so codes below if (file.exists()) does not work

What i want to know is how to create a file by using uri

thank for your help

来源:https://stackoverflow.com/questions/58521780/how-to-get-filepdf-jpg-docs-from-local-storage-and-create-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!