Ticherhaz FreePalestine
Ticherhaz FreePalestine

Reputation: 2387

Open PDF File from Download Manager Located from Download

I am using DownloadManager to download a file from FirebaseStorage.

First, I will get the downloadUrl from FirebaseStorage and proceed with DownloadManager

  1. As you can see codes below, this is where after I got the downloadUrl as url.
downloadManager = getSystemService(DOWNLOAD_SERVICE) as DownloadManager
val uri = Uri.parse(url)
val request = DownloadManager.Request(uri)

val folderName = File.separator + "MBITION" + File.separator + fileName + fileExtension

name = folderName

Log.i("???", "url: $url")
Log.i("???", "folderName: $folderName")

request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
request.setDestinationInExternalPublicDir(
        Environment.DIRECTORY_DOWNLOADS,
        folderName
 )

 enq = downloadManager!!.enqueue(request)

Logcat below shows the url and folderName value.

I/???: url: https://firebasestorage.googleapis.com/v0/b/mbition-2022.appspot.com/o/note%2F-N2_fRAhJXVshDjQMcrz?alt=media&token=936e1014-6c7a-4f46-89fd-5746eb6a9dbf
I/???: folderName: /MBITION/ICT600 - Chapter 3.pdf
  1. As you can see codes below, where onReceive from BroadcastReceiver is handled.
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE == action) {
        val downloadId = it.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0)
        val query = DownloadManager.Query()
        query.setFilterById(downloadId)
        val c: Cursor = downloadManager!!.query(query)
        if (c.moveToFirst()) {
               val columnIndex: Int = c.getColumnIndex(DownloadManager.COLUMN_STATUS)
               if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {

               val uriString: String = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))
               val file = File(uriString)
               val target = Intent(Intent.ACTION_VIEW)

               Log.i("???", "uriString:: $uriString")
               Log.i("???", "file:: $file")

               val uri = FileProvider.getUriForFile(
                      this@NoteActivity,
                      "$packageName.provider",
                       file
               )

               target.setDataAndType(uri, "application/pdf")
               target.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION

               val intentChooser = Intent.createChooser(target, "Open File")
               try {
                    startActivity(intentChooser)
               } catch (e: ActivityNotFoundException) {
                    Tools.showToast(
                        this@NoteActivity,
                        "You need to download PDF reader"
                    )
                }
            }
         }
   }

Below shows the Logcat.

I/???: uriString:: file:///storage/emulated/0/Download/MBITION/ICT600%20-%20Chapter%203-5.pdf
I/???: file:: file:/storage/emulated/0/Download/MBITION/ICT600%20-%20Chapter%203-5.pdf

This is my provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="external_files"
        path="." />
</paths>

I tried to open the file through the DownloadManager's Notification. It is working fine.

DownloadManager's Notification

Image below shows where the file is located inside the MBITION folder.

Location file from MBITION folder

Below is the error I got from Logcat.

2022-05-28 11:36:44.534 4999-4999/com.aaa.mbition E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.aaa.mbition, PID: 4999
    java.lang.IllegalArgumentException: Failed to find configured root that contains /file:/storage/emulated/0/Download/MBITION/ICT600%20-%20Chapter%203.pdf
        at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:800)
        at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:442)
        at com.aaa.mbition.ui.NoteActivity$onCompleteDownloadFile$1$onReceive$1.invoke(NoteActivity.kt:79)

Updated:

Refer to @blackapps 's suggestion. I tried to remove file:// by using replace like below:

        val uriString: String = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))
        val urlFixer = uriString.replace("file://","")

        val file = File(urlFixer)
        val target = Intent(Intent.ACTION_VIEW)

        Log.i("???", "uriString:: $uriString")
        Log.i("???", "file:: $file")
        Log.i("???", "urlFixer:: $urlFixer")
                        
        val uri = FileProvider.getUriForFile(
               this@NoteActivity,
               "$packageName.provider",
               file
        )

Below is the Logcat

I/???: uriString:: file:///storage/emulated/0/Download/MBITION/ICT600%20-%20Chapter%203-9.pdf
I/???: file:: /storage/emulated/0/Download/MBITION/ICT600%20-%20Chapter%203-9.pdf
I/???: urlFixer:: /storage/emulated/0/Download/MBITION/ICT600%20-%20Chapter%203-9.pdf

When I pressed OK button, it proceed to PDF Reader, a few milisecond, it kick me back to the apps.

Upvotes: 0

Views: 476

Answers (1)

Ticherhaz FreePalestine
Ticherhaz FreePalestine

Reputation: 2387

According to @blackapps answer is working. I fixed them by using replace.

val uriString: String = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))
val urlFixer = uriString.replace("file://", "").replace("%20", " ")
                        
val file = File(urlFixer)
val target = Intent(Intent.ACTION_VIEW)

Upvotes: 0

Related Questions