2

I want to send multiple selected files to the server through wifidirect. Problem is, only the first file selected is sent. According to resources I found online, the FileTransferService which extends IntentService handles asynchronous requests, and the service handles each Intent in turn using a worker thread, and stops itself automatically. Anyone has any idea why the other files are not sent to the server? Any help appreciated.

This is the activity where I start the file transfer service:

 ArrayList<String> uris = new ArrayList<String>();
     for( int i=0 ; i<f22.size(); i++ )
     {

       Uri uri = Uri.fromFile(f22.get(i));

       uris.add(uri.toString());

     }

 serviceIntent.setAction(FileTransferService.ACTION_SEND_FILE);
 for(int i=0; i<uris.size();i++)
     {
      serviceIntent.putExtra(FileTransferService.EXTRAS_GROUP_OWNER_ADDRESS,
         DeviceDetailFragment.info.groupOwnerAddress.getHostAddress());

      serviceIntent.putExtra(FileTransferService.EXTRAS_GROUP_OWNER_PORT, 8988);


      serviceIntent.putExtra(FileTransferService.EXTRAS_FILE_PATH, uris.get(i).toString());

      startService(serviceIntent);


     } 

This is the onHnadleintent function in FileTransferService

protected void onHandleIntent(Intent intent) {

    Context context = getApplicationContext();
    if (intent.getAction().equals(ACTION_SEND_FILE)) {

        String fileUri = intent.getExtras().getString(EXTRAS_FILE_PATH);
        String host = intent.getExtras().getString(EXTRAS_GROUP_OWNER_ADDRESS);
        Socket socket = new Socket();
        int port = intent.getExtras().getInt(EXTRAS_GROUP_OWNER_PORT);

        try {
            Log.d(WiFiDirectActivity.TAG, "Opening client socket - ");
            socket.bind(null);
            socket.connect((new InetSocketAddress(host, port)), SOCKET_TIMEOUT);

            Log.d(WiFiDirectActivity.TAG, "Client socket - " + socket.isConnected());
            OutputStream stream = socket.getOutputStream();
            ContentResolver cr = context.getContentResolver();
            InputStream is = null;


            try {
                is = cr.openInputStream(Uri.parse(fileUri));

            } catch (FileNotFoundException e) {
                Log.d(WiFiDirectActivity.TAG, e.toString());
            }
            DeviceDetailFragment.copyFile(is, stream);
            Log.d(WiFiDirectActivity.TAG, "Client: Data written");

        } 
            catch (IOException e) {
            Log.e(WiFiDirectActivity.TAG, e.getMessage());
        } finally {
            if (socket != null) {
                if (socket.isConnected()) {
                    try {
                        socket.close();

                    } catch (IOException e) {
                        // Give up
                        e.printStackTrace();
                    }
                }
            }
        }

    }
Nour Rteil
  • 45
  • 4

1 Answers1

5

This is not an answer to why the above code didnt work, but just an idea.

I had a similar requirement of sending multiple files over WiFi Direct. Also, I had to encrypt all files before sending.

The method I followed was to contain all the files that were to be exchanged in a particular "Directory" and send all the files in that directory in a single File-Exchange task's thread.

  • Obtain a BufferedOutputStream from the Socket and further a DataOutputStream from it
  • Write overall File Count to DOS(# of files in the directory)
  • [Inside each iteration] Write Name and Length of each File to DOS
  • Write the File to BOS
  • Do the reverse in Receiver to obtain the files

You could find some help from the below discussion link to achieve this:

How to send a list of files over a socket

Note: The limitation is that all the files you intend to send, has to be in this particular Directory/Sub-Directory

Hope this is of some use to you. Cheers.

Community
  • 1
  • 1
Hariharan Gandhi
  • 1,174
  • 9
  • 19
  • Thank you for your suggestion! I was able to send all the files by using a Handler delay with multiple runnables. – Nour Rteil Mar 22 '15 at 16:50