0

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

HyeonSeok
  • 589
  • 1
  • 5
  • 18
  • You seem to be using Retrofit or OkHttp. If so, use [the `InputStreamRequestBody` found in the second code snippet of this issue comment](https://github.com/square/okhttp/issues/3585#issuecomment-327319196) to create a `RequestBody` from a `Uri` using a `ContentResolver`. – CommonsWare Oct 23 '19 at 11:41
  • think you for your comment. but i found other way to create file. anyway thank you – HyeonSeok Oct 24 '19 at 08:20

0 Answers0