简体   繁体   中英

Android failed binder transaction when app goes on background

I have a fragment where I take 1 string in the new instance which is the base64 of a pdf file and then I convert it to an array of bytes. I can open pdf with a big size,over 30mb, but when the app goes on background, it crash and says "TransactionTooLargeException".I tried to set the 2 variables, the base64 string and the array of byte null onPause, or to use saveInstanceState.clear(), but the app continue to crash just when I close it with phone home button. I don't understand why if the variables are null, it crash anyway. Could I avoid the 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)
            }
        }
}
}

This is the error:

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)

Then, when I go back to the app with the task manager, it go back to the start activity...This appen only with huge pdf file.

in onPause pdf_view.close();

in onStart pdf_view.load();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM