4

I have picked the documents(PDF, DOC, DOCX) from the file manager and trying to get the path from Uri. But, I am getting an exception as (FileUriExposedException: exposed beyond app through Intent.getData())

I have used below code for getting file path from URI.

 fun getPathDOC(context: Context, uri: Uri): String? {
                    var uri = uri
                    val needToCheckUri = Build.VERSION.SDK_INT >= 19
                    var selection: String? = null
                    var selectionArgs: Array<String>? = null

                    // Uri is different in versions after KITKAT (Android 4.4), we need to
                    // deal with different Uris.

                    if (needToCheckUri && DocumentsContract.isDocumentUri(context.applicationContext, uri)) {
                        if (isExternalStorageDocument(uri)) {
                            val docId = DocumentsContract.getDocumentId(uri)
                            val split = docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
                            return Environment.getExternalStorageDirectory().toString() + "/" + split[1]

                        } else if (isDownloadsDocument(uri)) {
                            val id = DocumentsContract.getDocumentId(uri)

                            if (id != null && id.startsWith("raw:")) {
                                return id.substring(4)
                            }

                            val contentUriPrefixesToTry = arrayOf(
                                "content://downloads/public_downloads",
                                "content://downloads/my_downloads",
                                "content://downloads/all_downloads"
                            )

                            for (contentUriPrefix in contentUriPrefixesToTry) {
                                val contentUri =
                                    ContentUris.withAppendedId(Uri.parse(contentUriPrefix), java.lang.Long.valueOf(id!!))
                                try {
                                    val path = getDataColumn(context, contentUri, null, null)
                                    if (path != null) {
                                        return path
                                    }
                                } catch (e: Exception) {
                                }

                            }

                        } else if (isMediaDocument(uri)) {

                            val docId = DocumentsContract.getDocumentId(uri)
                            val split = docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
                            val type = split[0]
                            if ("image" == type) {
                                uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
                            } else if ("video" == type) {
                                uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
                            } else if ("audio" == type) {
                                uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
                            }
                            selection = "_id=?"
                            selectionArgs = arrayOf(split[1])
                        }
                    }
                    if ("content".equals(uri.scheme!!, ignoreCase = true)) {
                        val projection = arrayOf(MediaStore.Images.Media.DATA)
                        var cursor: Cursor? = null
                        try {
                            cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
                            val column_index = cursor!!.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
                            if (cursor.moveToFirst()) {
                                return cursor.getString(column_index)
                            }
                        } catch (e: Exception) {
                            println(e)
                        }

                    } else if ("file".equals(uri.scheme!!, ignoreCase = true)) {
                        return uri.path
                    }
                    return null
                }

I have tried :

How to get the file path from a URI that points to a PDF document?

https://www.dev2qa.com/how-to-get-real-file-path-from-android-uri/

My above code is working on some devices and giving an error in some devices kindly help me on this.

Baban Jadhav
  • 127
  • 5
  • have you handled permissions for reading external storage ? – a_local_nobody Jul 31 '19 at 06:48
  • Why do you need a path? You already have a `Uri` that points right to it. Furthermore, you can't send file `Uri`s externally anymore on newer Android versions. https://stackoverflow.com/q/38200282 – Mike M. Jul 31 '19 at 06:52
  • you likely don't need the path. Why you want to get it? – Vladyslav Matviienko Jul 31 '19 at 07:02
  • @a_local_nobody : Yes, I did this – Baban Jadhav Jul 31 '19 at 09:26
  • @MikeM. : I have already applied the solution given by you. But, not getting the path. I need the path to upload on AWS (S3 bucket) server. – Baban Jadhav Jul 31 '19 at 09:29
  • I didn't link that post to suggest it as a solution. I just linked it for the explanation, since it didn't seem as though you'd done any research on that error yet. Anyway, a `Uri` is not a file. It might point to a file, but it also might not. If the AWS thing absolutely has to have a file, then the safest thing for you to do would be to use `ContentResolver#openInputStream()` with the `Uri` to copy the document to a local file, then pass that file to the AWS API. Otherwise, if you can hand AWS a `Uri` instead, it'd be much better to just give it the one you got originally. – Mike M. Aug 01 '19 at 00:21

0 Answers0