how to get file(pdf, jpg, docs) from local

2020-05-06 12:46发布

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

0条回答
登录 后发表回答