创建并共享.txt文件TransactionTooLargeException

时间:2018-01-25 13:50:43

标签: java android file android-intent text-files

我正在尝试根据列表中的数据生成.txt文件。项目(行)数量至少为10.000。 问题不在文件本身内,因为文件是生成并保存在外部存储器上的,我可以对其进行RUD并通过共享意图共享它,例如通过Gmail。

7000项目的文件大小为aprox 70 KB,但问题是数据包大小为4818144字节,aprox为5 MB?

Lolipop和Marshmallow通过意图共享文件没有任何问题,但是Nougat与失败的BINDER TRANSACTION(包裹大小= 4818133)崩溃JavaBinder TransactionTooLarge异常

我在应用程序选择器中进行选择后发生了

崩溃,因此它启动了Gmail,但目前Gmail成为焦点 - >应用程序崩溃时提到异常,但文件是附件并且它是有效的(gmail保持焦点,我可以发送文件

String FILE_PATH = Environment.getExternalStorageDirectory().getPath() + "/" + Constants.INSTANCE.getInventoryFilePath();

private void generateFileTxt(String fileName) {

    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
        mCanWrite = true;

    if (!mCanWrite) {

        showDialogWritingError();
        this.dismiss();

    } else {

        String[] data = new String[mProductsList.size()];
        StringBuilder sb = new StringBuilder();
        String code, count;
        String newLine = "\n";
        String separator = ";";

        try {

            for (int i = 0; i < mList.size(); i++) {

                if (mList.get(i).getCode() != null) {

                    code = mList.get(i).getCode();
                    count = String.valueOf(mCountList.get(i));

                    sb.append(code).append(separator).append(count).append(newLine);

                    String row = sb.toString();
                    data[i] = row;
                    sb.setLength(0);

                }

            }

        } catch (Exception e) {
            Log.e(this.getClass().getSimpleName(), e.toString());
        }

        try {

            File dir = new File(FILE_PATH);
            dir.mkdir();

            File file = new File(dir + "/" + fileName + mTxtExtension);
            file.createNewFile();

            FileOutputStream streamOut = new FileOutputStream(file);
            OutputStreamWriter streamWriter = new OutputStreamWriter(streamOut);

            for (String row : data) streamWriter.append(row);

            streamWriter.flush();
            streamOut.flush();
            streamWriter.close();
            streamOut.close();

            showDialogShareTxt(fileName + mTxtExtension);

        } catch (Exception e) {
            Log.e(this.getClass().getSimpleName(), e.toString());
        }
    }

}

生成文件后启动共享意图的方法:

private void showDialogShareTxt(String fileName) {

    Toast.makeText(getActivity(), R.string.toast_file_saved, Toast.LENGTH_SHORT).show();

    File file = new File(FILE_PATH + "/" + fileName);

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/*");
    intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.getAbsolutePath()));

    startActivity(Intent.createChooser(intent, "Sending: " + fileName));

}

我(想)我在意图中发送文件名(参考,路径),为什么包裹大小为5MB? (我知道限制是1MB,或512KB不确定,但这太多了)

编辑1:堆栈跟踪

01-25 14:23:26.883 E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!  (parcel size = 4818144)
01-25 14:23:26.884 D/AndroidRuntime: Shutting down VM
01-25 14:23:26.884 E/AndroidRuntime: FATAL EXCEPTION: main
                                     Process: hr.vestok.inventorymanagement, PID: 26876
                                     java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 4818144 bytes
                                         at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3850)
                                         at android.os.Handler.handleCallback(Handler.java:754)
                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                         at android.os.Looper.loop(Looper.java:163)
                                         at android.app.ActivityThread.main(ActivityThread.java:6342)
                                         at java.lang.reflect.Method.invoke(Native Method)
                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
                                      Caused by: android.os.TransactionTooLargeException: data parcel size 4818144 bytes
                                         at android.os.BinderProxy.transactNative(Native Method)
                                         at android.os.BinderProxy.transact(Binder.java:620)
                                         at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3679)
                                         at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3842)
                                         at android.os.Handler.handleCallback(Handler.java:754) 
                                         at android.os.Handler.dispatchMessage(Handler.java:95) 
                                         at android.os.Looper.loop(Looper.java:163) 
                                         at android.app.ActivityThread.main(ActivityThread.java:6342) 
                                         at java.lang.reflect.Method.invoke(Native Method) 
                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880) 
                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770) 
01-25 14:23:26.892 E/UncaughtException: java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 4818144 bytes
                                            at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3850)
                                            at android.os.Handler.handleCallback(Handler.java:754)
                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                            at android.os.Looper.loop(Looper.java:163)
                                            at android.app.ActivityThread.main(ActivityThread.java:6342)
                                            at java.lang.reflect.Method.invoke(Native Method)
                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
                                         Caused by: android.os.TransactionTooLargeException: data parcel size 4818144 bytes
                                            at android.os.BinderProxy.transactNative(Native Method)
                                            at android.os.BinderProxy.transact(Binder.java:620)
                                            at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3679)
                                            at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3842)
                                            at android.os.Handler.handleCallback(Handler.java:754) 
                                            at android.os.Handler.dispatchMessage(Handler.java:95) 
                                            at android.os.Looper.loop(Looper.java:163) 
                                            at android.app.ActivityThread.main(ActivityThread.java:6342) 
                                            at java.lang.reflect.Method.invoke(Native Method) 
                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880) 
                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770) 
01-25 14:23:26.897 D/FA: Logging event (FE): app_exception(_ae), Bundle[{firebase_event_origin(_o)=crash, timestamp=1516886606893, fatal=1}]
01-25 14:23:27.166 I/Process: Sending signal. PID: 26876 SIG: 9

0 个答案:

没有答案