Andreas1234
Andreas1234

Reputation: 698

How to open file from URI?

I would like to crate, a file uploader fragment.

First of all, user picks up files, and after that I show the picked up files in a recycler view.

If the user clicks to recyclerView item, I would like to open the file.

So it is the problem, I tried a lot of answer, but I can't solve this.

I tried to open specific files for example pdf, and the pdf reader is opened, but couldn't find the file, and it happened when I want to open image fil as well

In the feature, when it is works, I would like to save and open different files for exaple images, documents etc

minSdk : 19 target: 30

onActivityResult: 
 if (resultCode == Activity.RESULT_OK) {
                    data?.let { it ->
                        var list = mutableListOf<DocumentView>()

                        if (it.clipData != null) {
                            for (i in 0 until it.clipData!!.itemCount) {
                                it.clipData?.getItemAt(i)?.uri?.let { uri ->
                                    viewModel.selectedFilesUri.add(uri)
                                }
                            }
                        } else {
                            it.data?.let { uri ->
                                viewModel.selectedFilesUri.add(uri)

                                getFileMetaData(uri)?.let { documentView ->
                                    list.add(documentView)
                                }
                            }
                        }

                        refreshFileList()
                    }
                }

    private fun refreshFileList(){
        val uris = viewModel.selectedFilesUri
        val list = mutableListOf<DocumentView>()

        uris.forEach { uri->
            getFileMetaData(uri)?.let {
                list.add(it)
            }
        }

        adapter.submitList(list)
    }

    private fun openFile(documentView: DocumentView, uri: Uri) {
        val intent = Intent(Intent.ACTION_VIEW)
        intent.setDataAndType(uri,"application/pdf")
        startActivity(intent)
    }

Manifest Provider: 
 <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_path" />
        </provider>

file_path:
<paths>
    <external-path name="external_files" path="."/>
</paths>

Upvotes: 1

Views: 1803

Answers (2)

J. M.
J. M.

Reputation: 115

You can set it this way easier

intent.setFlags(Intent.FLAG_GRANT_READ_PERMISSION);

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006584

You cited your code as being from onActivityResult(). My guess is that you are using ACTION_OPEN_DOCUMENT, or perhaps ACTION_GET_CONTENT, with startActivityForResult(), to get your Uri values.

You have rights to access that content from the Activity that received the result. Anything else needs to be granted permission. That includes third-party apps that you start via ACTION_VIEW.

Adding FLAG_GRANT_READ_URI_PERMISSION to your ACTION_VIEW Intent extends your read access rights to the content to whatever ACTION_VIEW activity winds up being started.

Upvotes: 1

Related Questions