简体   繁体   中英

Extream usage of RAM memory by ByteArrayOutputStream

So my problem sounds like this. I need to make a base64 encoded string of a file and for this, I use this method:

public String getStringFile(File f) {
    InputStream inputStream = null;
    String encodedFile= "", lastVal;
    try {
        inputStream = new FileInputStream(f.getAbsolutePath());

        byte[] buffer = new byte[10240];
        int bytesRead;

        ByteArrayOutputStream output = new ByteArrayOutputStream();
        Base64OutputStream output64 = new Base64OutputStream(output, Base64.DEFAULT);

        while ((bytesRead = inputStream.read(buffer)) != -1) {
            output64.write(buffer, 0, bytesRead);
        }

        output64.close();
        encodedFile =  output.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    lastVal = encodedFile;

    return lastVal;
}

and the thing is when I try to encode file something around 20 Mb (exact file size is 19,35 Mb) I get an OutOfMemoryException.

Before:

在此处输入图片说明

After:

在此处输入图片说明

What am I doing wrong and how can I fix this issue? Thanks in advance.

What am I doing wrong

You are attempting to encode a ~20MB file using base64 into a string. You will not have adequate heap space on many Android devices to have a single memory allocation that large.

how can I fix this issue?

If "this issue" is "create a ~26MB string of base64-encoded data", there is no reliable way to do this. You would have to find some other solution to whatever problem you are trying to solve by creating such a string.

ByteArrayOutputStream output = new ByteArrayOutputStream();
Base64OutputStream output64 = new Base64OutputStream(output, Base64.DEFAULT);

If you upload the base64 yourself with HttpUrlConnection you can do way with the ByteArrayOutputStream and replace above lines -while directly uploading- with

OutputStream output = con.getOutputStream();
Base64OutputStream output64 = new Base64OutputStream(output, Base64.DEFAULT);

Untested.

You could also directly base64 encode to a FileOutputStream of course.

OutputStream output = new FileOutputStream(......);
Base64OutputStream output64 = new Base64OutputStream(output, Base64.DEFAULT);

and then upload that file with POJO.

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