1
votes

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

2 Answers

1
votes

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
0
votes

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.