当应用程序进入后台时,Android的活页夹交易失败

时间:2019-05-03 11:01:04

标签: java android

我有一个片段,我在新实例中取一个字符串,该实例是pdf文件的base64,然后将其转换为字节数组。我可以打开超过30mb的大尺寸pdf,但是当应用程序在后台运行时,它崩溃并显示“ TransactionTooLargeException”。我尝试设置2个变量,即base64字符串和字节数组null onPause,或者使用saveInstanceState.clear(),但是仅当我使用手机主屏幕按钮将其关闭时,该应用才继续崩溃。 我不明白为什么变量如果为null,还是会崩溃。我可以避免saveInstanceState吗?

private const val BASE64_PDF = "base64_pdf"


class PdfViewerFragment : Fragment() {

private var basePdf: String? = null
private var pdfAsBytes: ByteArray? = null


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    arguments?.let {
        basePdf = it.getString(BASE64_PDF)
    }
}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_pdf_viewer, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    pdfAsBytes = basePdf.let { PdfUtils.convertBaseToByte(it) }
    if (pdfAsBytes != null) showPdf()

}


override fun onAttach(context: Context) {
    super.onAttach(context)

}

override fun onPause() {
super.onPause()
basePdf = null
pdfAsBytes = null
}

private fun showPdf() {
    pdf_view.fromBytes(pdfAsBytes)
        .enableSwipe(true)
        .swipeHorizontal(false)
        .enableDoubletap(true)
        .defaultPage(0)
        .enableAntialiasing(true)
        .onError { t ->
            showError()
            Log.d(TAG, t.message)
        }
        .load()
}

private fun showError() {
    Toast.makeText(context, "Errore durante il caricamento del PDF", Toast.LENGTH_LONG).show()
}

companion object {

    const val TAG = "PdfViewerFragmentTag"
    @JvmStatic
    fun newInstance(basePdf: String) =
        PdfViewerFragment().apply {
            arguments = Bundle().apply {
                putString(BASE64_PDF, basePdf)
            }
        }
}
}

这是错误:

2019-05-03 12:59:18.986 24071-24071/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.energym.app.energympalestre, PID: 24071
java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 93574832 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:201)
    at android.app.ActivityThread.main(ActivityThread.java:6810)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
 Caused by: android.os.TransactionTooLargeException: data parcel size 93574832 bytes
    at android.os.BinderProxy.transactNative(Native Method)
    at android.os.BinderProxy.transact(BinderProxy.java:473)
    at android.app.IActivityManager$Stub$Proxy.activityStopped(IActivityManager.java:3973)
    at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:144)
    at android.os.Handler.handleCallback(Handler.java:873) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:201) 
    at android.app.ActivityThread.main(ActivityThread.java:6810) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

然后,当我使用任务管理器返回到应用程序时,它又回到了开始活动...仅适用于巨大的pdf文件。

1 个答案:

答案 0 :(得分:0)

在onPause pdf_view.close();

在onStart pdf_view.load();