Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Intent.ACTION\u从内部存储器发送PDF文件仅适用于某些应用程序_Android_Pdf_Android Intent_Printing_Bluetooth - Fatal编程技术网

Android Intent.ACTION\u从内部存储器发送PDF文件仅适用于某些应用程序

Android Intent.ACTION\u从内部存储器发送PDF文件仅适用于某些应用程序,android,pdf,android-intent,printing,bluetooth,Android,Pdf,Android Intent,Printing,Bluetooth,我正试图通过发送一个Intent.ACTION\u发送到PrinterShare应用程序(我买了一个高级版本,声称可以从应用程序的内部存储打印文件),从应用程序的内部存储打印PDF文件 我已经看过了,并且正在将我的文件复制到内部缓存,这要感谢PrinterShare应用程序似乎正在获取我的文件,但不是“完全”——它无法呈现文件,当我打印时,它只是推出一个空文件 出于共享的目的,我可以选择其他应用程序,如Gmail和普通蓝牙,然后我可以将PDF文件作为电子邮件附件或通过蓝牙发送到我的个人手机上,并

我正试图通过发送一个Intent.ACTION\u发送到PrinterShare应用程序(我买了一个高级版本,声称可以从应用程序的内部存储打印文件),从应用程序的内部存储打印PDF文件

我已经看过了,并且正在将我的文件复制到内部缓存,这要感谢PrinterShare应用程序似乎正在获取我的文件,但不是“完全”——它无法呈现文件,当我打印时,它只是推出一个空文件

出于共享的目的,我可以选择其他应用程序,如Gmail和普通蓝牙,然后我可以将PDF文件作为电子邮件附件或通过蓝牙发送到我的个人手机上,并完美呈现。只是当我尝试将它分享给谷歌照片、闲逛以及PrinterShare应用程序时,出现了一些奇怪的错误

那么,如何调试一个只会偶尔发生的问题呢

在前面的代码中,我在内部存储器中创建了如下文件:

                val savePdf = File(view.filesDir, "printthis.pdf")
这是我的共享意向建设(Kotlin):

这是
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)
    }
}
@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)
    }
我的
ContentProvider
上唯一的自定义功能是
openFile
功能:

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

我缺少什么?

首先,PDF的MIME类型不是
image/*
。其次,在
flush()
之后和
close()
之前,在
FileOutputStream
上调用
getFD().sync()
,以确保在继续之前所有字节都写入磁盘。第三,请确保此I/O在后台线程上执行。第四,你的问题可能是
ContentProvider
中没有显示在你的问题中。啊,Commonware,当我们错过MIME类型之类的愚蠢事情时,你总是会来解围的。:)我已经添加了在我的自定义内容提供者中所覆盖的函数,您可能会考虑切换到使用<代码>文件提供者>代码>,而不是滚动您自己的提供者,看看是否有帮助。您先前的建议现在在PrPrStApp应用程序中呈现PDF,但现在我实际上有一个问题,连接到打印机(SuthHieldHassle)。尝试拔下蓝牙电缆并重新插入。:-)