0

I'm making a video with Google Glass and want to encode it into a Base64 String. When I'm trying to do this I always get an "OutOfMemoryException". The coding looks like the following:

        File mFile = new File(this.mNotification.getVideo());
        if (mFile.exists()) {
            try {
                InputStream is = new FileInputStream(mFile);
                try {
                    byte[] bytes = org.apache.commons.io.IOUtils.toByteArray(is);
                    byte[] value = org.apache.commons.codec.binary.Base64.encodeBase64(bytes);
                    this.mNotification.setVideo(new String(value));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

I only have the opportunity to send the video as a Base64 String...

user2858559
  • 39
  • 1
  • 9
  • 2
    It is unlikely that you will be able to have a whole video in memory in any form, let alone base64-encoded, as video files are usually large. You should have no problem in reading in bytes from the video file and writing out a file in base64. You would then need to rework whatever `mNotification` is to not require the entire video to be in memory as a string. – CommonsWare Nov 16 '14 at 23:01
  • Well actually I need the whole video and it should be converted completely into a base64 string because I need to send the string to the server. The problem occurs when trying to create the string. It's not the encoding I think – user2858559 Nov 16 '14 at 23:06
  • Then you are going to need to "send the string to the server" without loading the whole string into memory. Or, revamp the server to offer a different mechanism for uploading the video (e.g., `PUT` of the binary video to a server-supplied URL). – CommonsWare Nov 16 '14 at 23:07
  • Yeah I know what you mean but isn't this the common way to transform the file first to Base64? I mean I have the string and just send it. The question is just how to remove the "OutOfMemory" error. – user2858559 Nov 16 '14 at 23:13
  • 2
    "but isn't this the common way to transform the file first to Base64?" -- for *short things*. "I mean I have the string and just send it" -- and that is fine, for *short things*. Videos are not short. You have **very limited memory** in an Android app, worse on Android 4.4 and below where we're using Dalvik instead of ART. – CommonsWare Nov 16 '14 at 23:14
  • hmm ok... well the problem is my backend is a SAP system. I'm using Netweaver Gateway for communication, so I'm very limited with sending data etc. to the backend. Isn't there a special trick? – user2858559 Nov 16 '14 at 23:21
  • If SAP is providing you the Android API that you are calling, you will have to ask SAP how to upload videos via that Android API. – CommonsWare Nov 16 '14 at 23:27

0 Answers0