【问题标题】:Sending multiple files through wifidirect通过wifidirect发送多个文件
【发布时间】:2015-03-11 18:22:45
【问题描述】:

我想通过 wifidirect 将多个选定的文件发送到服务器。 问题是,只发送选择的第一个文件。根据我在网上找到的资源,扩展IntentService的FileTransferService处理异步请求,该服务使用工作线程依次处理每个Intent,并自动停止。任何人都知道为什么其他文件没有发送到服务器?任何帮助表示赞赏。

这是我启动文件传输服务的活动:

 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);


     } 

这是FileTransferService中的onHnadleintent函数

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();
                    }
                }
            }
        }

    }

【问题讨论】:

    标签: android intentservice wifi-direct


    【解决方案1】:

    这不是上面代码为什么不起作用的答案,而只是一个想法。

    我有通过 WiFi Direct 发送多个文件的类似要求。另外,我必须在发送之前加密所有文件。

    我遵循的方法是将要交换的所有文件包含在特定“目录”中,并在单个文件交换任务线程中发送该目录中的所有文件

    • Socket 获得一个BufferedOutputStream 并从它进一步获得一个DataOutputStream
    • 将整体文件计数写入 DOS(目录中的文件数)
    • [在每次迭代中] 将每个文件的名称和长度写入 DOS
    • 文件写入BOS
    • 在 Receiver 中执行相反的操作以获取文件

    您可以从下面的讨论链接中找到一些帮助来实现这一点:

    How to send a list of files over a socket

    注意:限制是您要发送的所有文件都必须位于此特定目录/子目录中

    希望这对您有所帮助。干杯。

    【讨论】:

    • 感谢您的建议!我能够通过使用具有多个可运行文件的处理程序延迟来发送所有文件。
    猜你喜欢
    • 2012-02-14
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    相关资源
    最近更新 更多