Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Open PDF Doc with Intent is not saving after close

the problem I'm facing is that when trying to save changes to a PDF document opened with this URI content://xx.xxx.xxx.fileprovider/external/Download/Sync/FileName.pdf, any change I make is not saving after closing the document. But when I use sample apps that create another format of URI like this one content://xx.xxx.xxx.file_provider/file%253A%252F%252F%252Fstorage%252Femulated%252F0%252FDownload%252FSync%252FFileName%252F%252FEYV.pdf the changes are saved successfully. I have also tried with ACTION_EDIT

    val file = File(fileModel.path)

    val uri = if (Build.VERSION.SDK_INT >= 24) {
          FileProvider.getUriForFile(activity?.applicationContext!!, "$APP_ID.fileprovider", file)
    } else {
          Uri.fromFile(file)
    }

    val mime = activity?.applicationContext!!.contentResolver.getType(uri)

    val pdfIntent = Intent(Intent.ACTION_VIEW)
    pdfIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
    pdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    pdfIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
    pdfIntent.setDataAndType(uri, mime)

    val packageManager = activity?.applicationContext?.packageManager
    val activities = packageManager!!.queryIntentActivities(pdfIntent, PackageManager.MATCH_DEFAULT_ONLY)

    for (resolvedIntentInfo in activities) {
        val packageName = resolvedIntentInfo.activityInfo.packageName
        activity?.applicationContext?.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
        activity?.applicationContext?.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
    }

    if (pdfIntent.resolveActivity(activity!!.packageManager) != null) {
        //startActivity(Intent.createChooser(pdfIntent, "Open"))
        startActivity(pdfIntent)
    }
like image 964
FantomasMex Avatar asked Feb 23 '20 18:02

FantomasMex


1 Answers

Please check this documentation and let me know if it helps.

// Request code for selecting a PDF document.
const val PICK_PDF_FILE = 2

fun openFile(pickerInitialUri: uri) {
    val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
        addCategory(Intent.CATEGORY_OPENABLE)
        type = "application/pdf"

        // Optionally, specify a URI for the file that should appear in the
        // system file picker when it loads.
        putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri)
    }

    startActivityForResult(intent, PICK_PDF_FILE)
}

Also, for editing any document this link might help.

val contentResolver = applicationContext.contentResolver

private fun alterDocument(uri: Uri) {
    try {
        contentResolver.openFileDescriptor(uri, "w")?.use {
            FileOutputStream(it.fileDescriptor).use {
                it.write(
                    ("Overwritten at ${System.currentTimeMillis()}\n")
                        .toByteArray()
                )
            }
        }
    } catch (e: FileNotFoundException) {
        e.printStackTrace()
    } catch (e: IOException) {
        e.printStackTrace()
    }
}
like image 136
Mohit Ajwani Avatar answered Nov 14 '22 06:11

Mohit Ajwani