Home > Net >  android.content.ActivityNotFoundException: No Activity found to handle Intent when opening pdf at an
android.content.ActivityNotFoundException: No Activity found to handle Intent when opening pdf at an

Time:10-04

I know there are many similar topics like this, but they are either outdated or do not work with pdfs.

My question is, how do I download a pdf from cloud firestore and save it into the internal storage? My normal method was to use downloadmanager and save the pdf into external storage, but since this does not work anymore because of scoped storage, I need to find a new way.

Currently, I only know how to create a temporary file and download the pdf from firestore into this temporary file, but not how to save it.

Old Method (with Downloadmanager)

class PDFDownloader(private val context: Context) {
    private val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager

    private val downloadDirectory = Environment.DIRECTORY_DOCUMENTS

    private val authority = "${BuildConfig.APPLICATION_ID}.fileprovider"

    private val pdfIntent = Intent(Intent.ACTION_VIEW).apply {
       flags = Intent.FLAG_ACTIVITY_NEW_TASK
       addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    }


    // DOWNLOADING THE PDF INTO EXTERNAL STORAGE
    suspend fun downloadPDF(pdfUri: String, fileName: String) {
       val pdfUrl = Firebase.storage.getReferenceFromUrl(pdfUri).downloadUrl.await()

       // NOT WORKING ANYMORE BECAUSE OF SCOPED STORAGE
       val request = DownloadManager.Request(pdfUrl)
           .setTitle(fileName)
           .setDestinationInExternalFilesDir(context, downloadDirectory, "$fileName.pdf")

       downloadManager.enqueue(request)
    }


    // RETRIEVING THE PDF FROM EXTERNAL STORAGE
    suspend fun getPDFFileAndOpen(fileName: String) {
       val regex = "$fileName.pdf"

       withContext(Dispatchers.IO) {
          val fileList = context.getExternalFilesDir(downloadDirectory)?.listFiles()
          val file = fileList?.find { it.name == regex }

          pdfIntent.setDataAndType(FileProvider.getUriForFile(context, authority, file), "application/pdf")

          context.startActivity(intent)
       }
   }
}

New Method (without Downloadmanager)

class PDFDownloaderWithoutManager(private val context: Context) {

   override suspend fun downloadPDF(uri: StorageReference, fileName: String) {
        withContext(Dispatchers.IO) {
            // Step 1: Creating a temporary file
            val tempFile =  File(context.filesDir, "$fileName.pdf")

            // Step 2: Downloading the pdf from cloud-firestore into tempFile
            uri.getFile(tempFile).await()

            // Step 3: Saving the file into internal storage
            // OR SAVING INTO EXTERNAL STORAGE WITH SCOPED STORAGE 
            // (I take whats easier)
            ?????????????????????????????????????????????????????
        }
    }
}

   // RETRIEVING THE PDF FROM INTERNAL STORAGE
    suspend fun getPDFFileAndOpen(fileName: String) {
       val regex = "$fileName.pdf"
       
       ????????????????????????????????????????????????????????????
   }

There is also a method uri.getFile("URI TO SAVE FILE TO), but I don't know how to use that either.

Edit

Using download manager works as intended, and it successfully downloads the file from firestore (yay). The only problem I have is, that I can't open the pdf anymore at android 11. I get the following error:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://com.example.app.fileprovider/external_files/Documents/Kalibrierung und Überprüfung.pdf typ=application/pdf flg=0x10000001 }
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2067)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1727)
        at android.app.ContextImpl.startActivity(ContextImpl.java:1023)
        at android.app.ContextImpl.startActivity(ContextImpl.java:994)
        at android.content.ContextWrapper.startActivity(ContextWrapper.java:403)
        at com.example.app.business.domain.validator.document.FileLegacyValidator.openPdf(FileLegacyValidator.kt:38)
        at com.example.app.business.domain.validator.document.FileLegacyValidator.openPDFFileOrNull(FileLegacyValidator.kt:35)
        at com.example.app.presentation.documents.DocumentViewModel$setStateEvent$1$1.invokeSuspend(DocumentViewModel.kt:36)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

This is not working: context.startActivity(intent)

CodePudding user response:

Edit: Answer not relevant after your latest edit to question.

If you're going to use external storage, here's how to do it:

val isExternalStorageWritable =
    (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED)
if (!isExternalStorageWritable ) {
    // handle error: Storage not available
    return
}

val rootFolder = context.getExternalFilesDir(null)
val downloadsFolder = File(rootFolder, "downloads")
val isDownloadsFolderCreated = downloadsFolder.mkdirs()
if (!isDownloadsFolderCreated) {
    // handle error: unable to create folder
    return
}

val tempInputStream = FileInputStream(tempFile)

val targetFile = File(downloadsFolder, "targetFileName.pdf")
val isTargetFileCreated = targetFile.createNewFile()
if (!isTargetFileCreated) {
    // handle error: unable to create file
    return
}

FileOutputStream(targetFile).use { outputStream -> 
    tempInputStream.copyTo(outputStream)
}

Make sure to call this from a background thread or IO coroutine.

CodePudding user response:

I've fixed this problem. I always thought that my emulator had a PDF reader already installed (since the other emulator had it too), but it didn't. The activity couldn't be opened because there was no PDF reader.

Fix: Install PDF Reader.

  • Related