我正在尝试通过蓝牙发送图像。成功连接设备,选择和发送文件后,它似乎没问题。但接收文件的手机显示文件的名称,但打开时文件似乎是空的。
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
Log.d(TAG, "create ConnectedThread");
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
File pictureFile = getDir();
Bitmap bitmap = BitmapFactory.decodeStream(mmInStream);
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
fos.flush();
fos.close();
mHandler.obtainMessage(BluetoothChat.MESSAGE_FILE, -1,
-1, pictureFile.getAbsolutePath().getBytes())
.sendToTarget();
} catch (Exception e) {
e.printStackTrace();
}
}
private File getDir() {
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
"Test");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(TAG, "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
Log.d("Gauss", "Saving to: " + mediaFile.getAbsolutePath());
return mediaFile;
}
请帮忙吗?