gpt4 book ai didi

java - Android - 关闭后不保存 Intent 打开 PDF 文档

转载 作者:行者123 更新时间:2023-12-01 11:19:37 25 4
gpt4 key购买 nike

我面临的问题是,当尝试将更改保存到使用此 URI 打开的 PDF 文档时
内容://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

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

最佳答案

请查看this documentation让我知道它是否有帮助。

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

此外,用于编辑任何文档 this link可能有帮助。
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()
}
}

关于java - Android - 关闭后不保存 Intent 打开 PDF 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60365349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com