之前用流的方式和contentProvide的方式发图片。结果流的方式在接收端出来的图片有问题,而contentProvide方式由于BluetoothShare 类不支持 android 4.1 及以上,所以放弃了。
现在改用以下方法:
ArrayList<Uri> a = new ArrayList<Uri>();
for (int i = 0; i < list.size(); i++) {a.add(Uri.fromFile(new File(list.get(i))));
}
Intent intent = new Intent();intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.setComponent(new ComponentName(
"com.android.bluetooth",
"com.android.bluetooth.opp.BluetoothOppLauncherActivity"));
intent.setType("*/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, a);
startActivity(Intent.createChooser(intent, "发送图片"));
求助:两个蓝牙设备连接上后,用流的方式传输文件,结果失败了。
到处寻求助攻无果。不知道是编码模式有问题,还是流处理有问题。如果有了解人士,求指导,发送端代码如下:
is = new FileInputStream(path);
byte[] b = new byte[1024];
int len=0;
len = is.read(b);
while(len!=-1){
BoothService.write(b);
Thread.sleep(100);
len = is.read(b);
}
接收端代码如下,msg.obj是接收到的数组,发送文本完全没问题:
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(str+"/"+systemTime+".jpg")));
byte[] readBuf = (byte[]) msg.obj;
bos.write(readBuf );