我正在尝试从应用程序的内部存储中打印PDF文件,方法是将Intent.ACTION_SEND发送到PrinterShare应用程序(我已经购买了高级版本,声称可以从应用程序的内部存储中打印文件)。
我查看了this,this,并且由于this而将我的文件复制到内部缓存,似乎PrinterShare应用程序正在获取我的文件,但不是“完全” “ - 它无法渲染文件,当我打印时,它只是推出一个空文件。
根据共享意图,我可以选择其他应用程序,如Gmail和普通蓝牙,然后我可以将PDF文件作为电子邮件附件或通过蓝牙发送到我的个人手机,它可以完美呈现。只是当我尝试将其分享给Google相册,环聊以及PrinterShare应用时,这有点奇怪。
那么你如何调试有时只发生的问题呢?
在我的代码的早期,我在内部存储中创建文件,如下所示:
val savePdf = File(view.filesDir, "printthis.pdf")
这是我的共享意图构建(Kotlin):
override fun printPdf() {
copyFileToInternal()
val uri = Uri.parse("content://my.package.android.app/printthis.pdf")
Log.d("Print PDF from $uri")
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.type = "image/*"
shareIntent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
shareIntent.putExtra(Intent.EXTRA_STREAM, uri)
(view as Context).startActivity(Intent.createChooser(shareIntent, "Print PDF"))
}
这是copyFileToInternal
函数:
private fun copyFileToInternal() {
try {
val inputStream = (view as Context).openFileInput("pop.pdf")
val cacheDir = (view).cacheDir
val outFile = File(cacheDir, "pop.pdf")
val os = FileOutputStream(outFile.absolutePath)
val buff = ByteArray(1024)
var len: Int
len = inputStream.read(buff)
while (len > 0) {
os.write(buff, 0, len)
len = inputStream.read(buff)
}
os.flush()
os.close()
inputStream.close()
} catch (e: IOException) {
e.printStackTrace()
Log.e("Could not copy file to internal cache. ", e)
}
}
我的ContentProvider
上唯一的自定义是openFile
功能:
@Throws(FileNotFoundException::class)
override fun openFile(uri: Uri, mode: String): ParcelFileDescriptor? {
val cacheDir = context.cacheDir
val privateFile = File(cacheDir, "printthis.pdf")
return ParcelFileDescriptor.open(privateFile, ParcelFileDescriptor.MODE_READ_ONLY)
}
我错过了什么?