1

我是 Android 编程的新手。我正在尝试开发具有文件传输功能的信使应用程序。但是在重新传输文件时遇到困难。

viewer.java**

Button transfer = (Button) findViewById(R.id.btnimage);    
transfer.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);                
            }
        });

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode == RESULT_OK) {
                if (requestCode == SELECT_PICTURE) {
                    Uri selectedImageUri = data.getData();
                    selectedImagePath = getPath(selectedImageUri);
                    System.out.println("Image Path : " + selectedImagePath);
                    XMPPClient.getInstance().SendFile(to, selectedImagePath);
                }
            }
        }

        public String getPath(Uri uri) {
            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = managedQuery(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }

然后我有 XMPPCLIENT.java

public void ReceiveFile() {
        Thread thread = new Thread() {
            public void run() {
                ServiceDiscoveryManager sdm = ServiceDiscoveryManager
                        .getInstanceFor(getConnection());

                if (sdm == null)
                    sdm = new ServiceDiscoveryManager(getConnection());

                sdm.addFeature("http://jabber.org/protocol/disco#info");

                sdm.addFeature("jabber:iq:privacy");

                // Create the file transfer manager
                final FileTransferManager managerListner = new FileTransferManager(
                        getConnection());

                FileTransferNegotiator.setServiceEnabled(getConnection(), true);

                Log.i("File transfere manager", "created");

                // Create the listener
                managerListner
                        .addFileTransferListener(new FileTransferListener() {
                            public void fileTransferRequest(
                                    final FileTransferRequest request) {

                                Log.i("Recieve File",
                                        "new file transfere request  new file transfere request   new file transfere request");

                                Log.i("file request",
                                        "from" + request.getRequestor());

                                IncomingFileTransfer transfer = request
                                        .accept();

                                Log.i("Recieve File alert dialog", "accepted");
                                try {

                                    transfer.recieveFile(new File("/sdcard/"
                                            + request.getFileName()));

                                    while (!transfer.isDone()
                                            || (transfer.getProgress() < 1)) {

                                        Thread.sleep(1000);
                                        Log.i("Recieve File alert dialog",
                                                "still receiving : "
                                                        + (transfer
                                                                .getProgress())
                                                        + " status "
                                                        + transfer.getStatus());

                                        if (transfer.getStatus().equals(
                                                Status.error)) {
                                            // Log.i("Error file",
                                            // transfer.getError().getMessage());
                                            Log.i("Recieve File alert dialog",
                                                    "cancelling still receiving : "
                                                            + (transfer
                                                                    .getProgress())
                                                            + " status "
                                                            + transfer
                                                                    .getStatus());
                                            transfer.cancel();

                                            break;
                                        }

                                    }

                                } catch (XMPPException e) {

                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }
                        });

            }
        };
        thread.start();
    }

    public void SendFile(final String Receiver, final String Directory) {
        Thread thread = new Thread() {
            public void run() {

            ServiceDiscoveryManager sdm = ServiceDiscoveryManager
            .getInstanceFor(getConnection());
            if (sdm == null)
                sdm = new ServiceDiscoveryManager(getConnection());
            sdm.addFeature("http://jabber.org/protocol/disco#info");
            sdm.addFeature("jabber:iq:privacy");
            // Create the file transfer manager
            FileTransferManager manager = new FileTransferManager(
                    mConnection);
            FileTransferNegotiator
            .setServiceEnabled(getConnection(), true);

            // Create the outgoing file transfer
            OutgoingFileTransfer transfer = manager
            .createOutgoingFileTransfer(getJIDofUserID(Receiver));
            sendMessage(getJIDofUserID(Receiver), "image");
            Log.i("transfere file", "outgoingfiletransfere is created");
            try {
                OutgoingFileTransfer.setResponseTimeout(30000);
                transfer.sendFile(new File(Directory), "Description");
                Log.i("transfere file", "sending file");
                while (!transfer.isDone()) {
                    try {
                        Thread.sleep(1000);
                        Log.i("transfere file", "sending file status "
                                + transfer.getStatus() + "progress: "
                                + transfer.getProgress());
                        if (transfer.getStatus() == org.jivesoftware.smackx.filetransfer.FileTransfer.Status.error) {
                            transfer.cancel();
                            Log.e("",transfer.getError()+" error");
                            break;
                        }
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        Log.e("aaaaaaaaaaaaaaa","aaaa"+e);
                        e.printStackTrace();
                    }
                }
            } 
            catch (XMPPException e) {
                // TODO Auto-generated catch block
                Log.e("aaaaaaaaaaaaaaa","aaaa"+e);
                e.printStackTrace();
            }
            Log.i("transfere file", "sending file done");
        }
        };
        thread.start();
    }

所以我的问题是:

  1. 我使用的连接是否与聊天连接相同(默认端口:5222)?
  2. 当我触发sendfile(final String Receiver, final String Directory)功能时,接收者何时以及如何获得任何通知?
4

2 回答 2

0

要回答您的问题:

  1. 这取决于用于文件传输的方法。XMPP 知道几个。你可以在这里阅读更多关于它的信息https://github.com/igniterealtime/Smack/wiki/Smack-XMPP-File-Transferhttp://xmpp.org/xmpp-protocols/xmpp-extensions/

  2. 接收实体始终必须首先确认文件传输。但这取决于其他客户端是如何实现的,例如是否有一个弹出窗口说有一个传入的文件传输。另一端的客户端也可能配置为自动接受所有请求。

于 2013-04-26T14:42:30.980 回答
0

首先将以下配置添加到您的 xmpp 配置类中。

ProviderManager.getInstance().addIQProvider('query','http://jabber.org/protocol/bytestreams', new BytestreamsProvider());
2
ProviderManager.getInstance().addIQProvider('query','http://jabber.org/protocol/disco#items', new DiscoverItemsProvider());
3
ProviderManager.getInstance().addIQProvider('query','http://jabber.org/protocol/disco#info', new DiscoverInfoProvider());

然后在开火服务器中添加或编辑以下三个属性。

  1. xmpp.proxy.enabled – 真
  2. xmpp.proxy.port – 7777
  3. xmpp.proxy.externalip - [可公开访问的主机或 ip]

请进行这些更改,然后重试。

于 2013-05-02T06:20:45.230 回答