arrays - Best way to handle large file in android -
i'm trying 'pdf' file local folder , converting byte array shown below. pdf large throws me 'out of memory exception'
so overcome issue users upload files more 10-20 mb files.
conversion file byte array:
public static string convertfiletobytearray(file f) { byte[] bytearray = null; try { inputstream inputstream = new fileinputstream(f); bytearrayoutputstream bos = new bytearrayoutputstream(); byte[] b = new byte[1024 * 11]; int bytesread = 0; while ((bytesread = inputstream.read(b)) != -1) { bos.write(b, 0, bytesread); } bytearray = bos.tobytearray(); log.e("byte array", ">" + bytearray); } catch (ioexception e) { e.printstacktrace(); } return base64.encodetostring(bytearray, base64.no_wrap); }
exception occurred:
java.lang.outofmemoryerror @ java.io.bytearrayoutputstream.expand(bytearrayoutputstream.java:91) @ java.io.bytearrayoutputstream.write(bytearrayoutputstream.java:201) @ android.util.base64outputstream.internalwrite(base64outputstream.java:141) @ android.util.base64outputstream.write(base64outputstream.java:101) @ com.sampleapp.testapp.testapp.mainactivity.convertfiletobytearray(mainactivity.java:547) @ com.sampleapp.testapp.testapp.mainactivity.onactivityresult(mainactivity.java:401) @ android.app.activity.dispatchactivityresult(activity.java:5643) @ android.app.activitythread.deliverresults(activitythread.java:3677) @ android.app.activitythread.handlesendresult(activitythread.java:3724) @ android.app.activitythread.access$1400(activitythread.java:175) @ android.app.activitythread$h.handlemessage(activitythread.java:1356) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:146) @ android.app.activitythread.main(activitythread.java:5602) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1283) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1099) @ dalvik.system.nativestart.main(native method)
the solution have 2 streams @ same time, 1 read memory, 1 send server. read chunk of data intermediate buffer (e.g. 4096 bytes) , send it, , repeat using single, small buffer.
also, suggest use kind of library that, well-know retrofit github.com/square/retrofit
there plenty of examples on how use in blogs, tutorials... this tutorial looks quite good, , targets retrofit 2.x.
Comments
Post a Comment