首页 > 解决方案 > 打开 PDF-s、XLS、DOC 时事务过大

问题描述

我的应用程序的一部分是将设备从服务器接收的文档显示为字节数组。我通过使用 Intent.ACTION_VIEW 解决了这个问题,但它显示了一个错误,说事务太大,因为文件大约 9 MB。

我读到了这个错误,并且几乎是不言自明的。有没有办法将此字节数组保存为本地文件并仅将路径传递给所需的应用程序?我可以通过什么方式解决这个问题?

另外,我可以使用 Apache 库在应用程序中呈现这些文件吗?

代码:

        if(response.isSuccessful){
                var name = name1
                val fileBytes = response.body()?.bytes()
                if (fileBytes == null) {
                    Toast.makeText(context, context.getString(R.string.no_data), Toast.LENGTH_LONG).show()
                    return
                }
                if (name.isEmpty() || name == "") name = "testFile"


                val mFolder = File(context.filesDir, "/androidapp")
                val mFile = File(mFolder.absolutePath, "$name.${mimeType.split("/")[1]}")

                if(!mFolder.exists()){
                    mFolder.mkdir()
                }

                if (!mFile.exists()) {
                    Log.e(TAG, "showPrintPreview: File does not exists")
                    try {
                        Log.d(
                            TAG,
                            "showPrintPreview: File created" + mFile.createNewFile()
                        )
                        Log.d(
                            TAG,
                            "showPrintPreview: " + mFile.isDirectory
                        )
                    } catch (e: IOException) {
                        e.printStackTrace()
                    }
                }
                try {
                    val fos = FileOutputStream(mFile)
                    fos.write(fileBytes)
                    fos.flush()
                    fos.close()
                } catch (e: IOException) {
                    e.printStackTrace()
                    Log.d("showPrintPreview: ", e.message)
                    // handle exception
                }
                try {
                    val uri: Uri = getUriForFile(context, context.applicationContext.packageName,
                        mFile
                    )
                    val intent = Intent(Intent.ACTION_VIEW)
                    intent.setDataAndType(uri, mimeType)
                    //            
                   intent.setDataAndType(Uri.fromFile(file), "application/pdf");
                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                    //            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent)
                } catch (ex: ActivityNotFoundException) {
                    showNoPrintAppDialog(context, mimeType.split("/")[1])
                }
            }`

错误:

java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 996448 bytes
    at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:160)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7078)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
 Caused by: android.os.TransactionTooLargeException: data parcel size 996448 bytes
    at android.os.BinderProxy.transactNative(Native Method)
    at android.os.BinderProxy.transact(Binder.java:1145)
    at android.app.IActivityManager$Stub$Proxy.activityStopped(IActivityManager.java:3891)
    at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:144)

标签: androidandroid-studiopdfkotlindocument

解决方案


推荐阅读