【发布时间】:2011-12-23 17:50:50
【问题描述】:
大家好,根据我之前提出的一个问题,我已经能够将文件转换为字节数组,以使用 write 方法:
public void sendFile(){
Log.d(TAG, "sending data");
InputStream inputStream = null;
Uri uri = Uri.withAppendedPath(MediaStore.Images.Media.INTERNAL_CONTENT_URI, "" + filesID.get(0));
Log.d(TAG, "obtained input stream here in recentDevices Activity");
try {
inputStream = getContentResolver().openInputStream(uri);
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int buffersize = 1024;
byte[] buffer = new byte[buffersize];
int len = 0;
while((len = inputStream.read(buffer)) != -1){
byteBuffer.write(buffer, 0, len);
}
Log.d(TAG, "sending data to connected thread");
bluetooth_service.write(byteBuffer.toByteArray());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
从 BluetoothChatService 示例中,我在连接的线程中有这个写入方法:
public void write(byte[] out) {
// Create temporary object
ConnectedThread r;
// Synchronize a copy of the ConnectedThread
synchronized (this) {
r = connectedThread;
}
// Perform the write unsynchronized
r.write(out);
}
/* Call this from the main Activity to send data to the remote device */
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes); // basically stuck here. nothing happens here
} catch (IOException e) { }
mHandler.sendEmptyMessage(RecentDevices.TRANSFER_COMPLETED);
}
我基本上卡在连接线程的写入方法中,那里似乎没有发生任何事情。在处理程序发送 TRANSFER_COMPLETED 方法后,我放置了一个进度对话框以将其关闭,但程序只是卡在这里。已尝试发送到 pc 或其他 android 设备。没运气!任何人都取得了任何成功或解决方案?
【问题讨论】:
标签: android