Android-关闭后无法保存带有Intent的PDF文档 [英] Android - Open PDF Doc with Intent is not saving after close

查看:204
本文介绍了Android-关闭后无法保存带有Intent的PDF文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临的问题是,当尝试将更改保存到使用此URI打开的PDF文档中时 content://xx.xxx.xxx.fileprovider/external/Download/Sync/FileName.pdf ,关闭文档后,我所做的任何更改均未保存.但是,当我使用示例应用程序来创建另一种URI格式时,例如 content://xx.xxx.xxx.file_provider/file%253A%252F%252F%252Fstorage%252Femulated%252F0%252FDownload%252FSync%252FFileName% 252F%252FEYV.pdf 更改已成功保存.我也尝试过使用 ACTION_EDIT

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)
    }

推荐答案

请检查此文档,并告诉我是否有帮助.

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()
    }
}

这篇关于Android-关闭后无法保存带有Intent的PDF文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆