如何浏览文件并通过蓝牙发送到连接的设备?

时间:2013-11-19 19:59:15

标签: android file sockets bluetooth chat

Obs:我知道这里有很多问题,但是没有一个人能够做到我想要的,认真的,所以不要因为Duplicate问题而进行投票,谢谢。

好吧,我有一个应用程序,它有所有必要的Bluetooth类用于连接和管理等。我的应用程序是Chat通过Bluetooth,我可以发送字符串消息到另一个设备和其他设备也可以发送给我。 ServerClient

以下是我发送Message(工作)

的方法
public boolean sendMessageByBluetooth(String msg){
    try {
        if(dataOutputStream != null){
            dataOutputStream.write(msg.getBytes());
            dataOutputStream.flush();
            return true;
        }else{
            sendHandler(ChatActivity.MSG_TOAST, context.getString(R.string.no_connection));
            return false;
        }
    } catch (IOException e) {
        LogUtil.e(e.getMessage());

        sendHandler(ChatActivity.MSG_TOAST, context.getString(R.string.failed_to_send_message));
        return false;
    }
}

如果可能的话,我想要一个类似于该方法的方法,Send Files可以使用相同的源来实现File Transfer

但我需要在我的应用上设置一个File Transfer的功能,它有一个Browse按钮来搜索Device上的文件,然后选择它并发送到配对设备,简单。

太,我想看到ProgressBar填充给用户看看发送文件需要多长时间:)

我怎么能这样做?

- 编辑 -

我正在尝试File Transfer这就是我所做的:

FileActivity.java

要连接的onCreate事件:

verifyAndActivateBluetooth(); //this function is same used on ChatActivity it is working.
registerFilter(); //this function is same used on ChatActivity it is working.
chatBusinessLogic.startFoundDevices(); //this function is same used on ChatActivity and it is working.

然后我选择可见的另一台设备并连接成功。

连接后然后..

按钮Browse - >意图显示文件资源管理器:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, FILE_FOUND); 

FileActivity onActivityResult switch(requestCode)

case FILE_FOUND:
    filename = data.getData().getPath(); //here was the problem of error 1. now fixed
    if(filename.trim().length() > 0){

        if(chatBusinessLogic.sendFile(filename)){
            lblFilename.setText(filename);
            toastUtil.showToast("Success");
        }
    }else{
        toastUtil.showToast("fail");
    }

chatBusinessLogic.sendFile(filename)

public boolean sendFile(String file){

    if(bluetoothComunication != null){
        return bluetoothComunication.sendFileByBluetooth(file);
    }else{
        return false;
    }
}   

bluetoothComunication.sendFileByBluetooth(file)

public boolean sendFileByBluetooth(String fpath){

File f = new File(fpath);
int n  = 0;
byte b[] = new byte[(int) f.length()];
try
{
    FileInputStream fileInputStream = new FileInputStream(new File(fpath)); //ERROR FIXED
    DataOutputStream dataOut = new DataOutputStream(dataOutputStream);
    while (-1 != (n = fileInputStream.read(b))){
        dataOutputStream.write(b, 0, n);
    }
    dataOut.flush();
    fileInputStream.close();
    dataOut.close();
    return true;
}catch(IOException e)
{
    LogUtil.e("Error converting file");
    LogUtil.e(e.getMessage());
    return false;
}

我收到此错误(现在已修复): 如果我为物理路径选择android native方法:

/content:/com.estrongs.files/mnt/sdcard/calc-history.txt: open failed: ENOENT (No such file or directory)

如果我选择物理路径的替代方法:

/file:/mnt/sdcard/calc-history.txt: open failed: ENOENT (No such file or directory)

上述错误是使用案例filename = data.getData().getPath();上的FILE_FOUND而非filename = data.getDataString();

修复的

现在我在这一行还有另一个错误:

dataOutputStream.write(b, 0, n);

错误信息是:Transport endpoint is not connected,我该怎么做?

2 个答案:

答案 0 :(得分:2)

要浏览您的设备,请使用aFileChooser之类的内容(除此之外还有很多内容。谷歌吧;)。
您可以像使用消息一样使用dataOutputStream/dataInputStream发送/接收文件。
你有没有试过ACTION_SEND - 意图?它也可以这样工作。
ProgressBar是一个可以使用的Android小部件,这应该不是问题。
如果您需要一般信息,请使用Android Developers Guide(您可能已经这样做了)。几乎所有关于android的东西都可以在那里找到,你只需要看一下。

编辑:(来自评论)

FileInputStream fin = new FileInputStream(<filepath>);
DataOutputStream dOut = new DataOutputStream(out);
int n = 0;
byte[] buffer = new byte[1024 * 4];
while (-1 != (n = fin.read(buffer))) {
       dOut.write(buffer, 0, n);
}
dOut.flush();
fin.colse();
dOut.close();
希望这是可以理解的。

答案 1 :(得分:0)

要从图库中选择文件,我会这样做:

Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 1);

要从图库中选择文件路径并在不同的应用程序(如蓝牙,gmail,whatsapp等)上共享文件路径,我在onActivityResult中执行此操作:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        Uri selectedImage = data.getData();
                   Intent sendIntent = new Intent();
                            sendIntent.setType("file/*");
                            sendIntent.setAction(Intent.ACTION_SEND);
                            sendIntent.putExtra(Intent.EXTRA_STREAM,
                                    selectedImage);
                            // startActivity(Intent.createChooser(sendIntent));
                            startActivity(Intent.createChooser(sendIntent,
                                    "Share file via:"));
                 }
catch(Exception e)
{
}
}