3

I am working on transfer file from server to client example.When i am trying to transfer large file (in example i have 500mb video file) then it shows error Android:java.lang.OutOfMemoryError Failed to allocate a 360318346 byte allocation with 5802592 free bytes and 87MB until OOM. Please help me how to solve this error.

Server side some peace of code

 public class FileTxThread extends Thread {
    Socket socket;
    FileTxThread(Socket socket){
        this.socket= socket;
    }

    @Override
    public void run() {
        File file = new File(Environment.getExternalStorageDirectory(),"RAHASYA.mp4");


        byte[] bytes = new byte[(int) file.length()];


        BufferedInputStream bis;
        try {
            bis = new BufferedInputStream(new FileInputStream(file));
            bis.read(bytes, 0, bytes.length);
            OutputStream os = socket.getOutputStream();
            os.write(bytes, 0, bytes.length);
            os.flush();
           // socket.close();

            final String sentMsg = "File sent to: " + socket.getInetAddress();

            MainActivity.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    Toast.makeText(MainActivity.this,sentMsg,Toast.LENGTH_LONG).show();

                }});

Client side some peace of code

private class ClientRxThread extends Thread {
    String dstAddress;
    int dstPort;

    ClientRxThread(String address, int port) {
        dstAddress = address;
        dstPort = port;
    }

    @Override
    public void run() {
        Socket socket = null;

        try {
            socket = new Socket(dstAddress, dstPort);

            out.println("Sockt "+socket.getInetAddress());

            File file = new File(Environment.getExternalStorageDirectory(),"RAHASYA.mp4");


            byte[] bytes = new byte[1024];


            InputStream is = socket.getInputStream();
         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            while (true) {
                int bytesRead = is.read(bytes);
                if (bytesRead < 0) break;
                bos.write(bytes, 0, bytesRead);
                // Now it loops around to read some more.
            }
            bos.close();


            MainActivity.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    Toast.makeText(MainActivity.this,"Finished",Toast.LENGTH_LONG).show();
                }});

2 Answers 2

2

your byte array on server side is too large , change it smaller and try ?
for reference:

  File file = new File(Environment.getExternalStorageDirectory(),"RAHASYA.mp4");
    byte[] bytes = new byte[2048];
    BufferedInputStream bis;
    try {
        bis = new BufferedInputStream(new FileInputStream(file));
        OutputStream os = socket.getOutputStream();
        int read = bis.read(bytes, 0, bytes.length);
        while (read!=-1){
            os.write(bytes, 0, read);
            os.flush();
            read = bis.read(bytes, 0, bytes.length);
        }

        //...and so on
Sign up to request clarification or add additional context in comments.

3 Comments

how to change. i am beginner , i confused where to have change in my server side code please help me.Thanks!
change this: byte[] bytes = new byte[(int) file.length()];
please can u tell me which code i have to add on this line byte[] bytes = new byte[(int) file.length()]. Thanks!
0

Your problem is the line

byte[] bytes = new byte[(int) file.length()];

You are creating an array of the size of the whole file. You need to read a bit at a time.

2 Comments

yes sir , but please give me some hint of how to read file in bit, i did try but i dont know its right or wrong because its not working ,please help . Thanks sir.
Do what you are doing on the client side, but on the server side (changing reads to writes obv).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.