使用 openFileOutput 时在内部类中正确使用上下文?
我正在尝试调整 Android BluetoothChat 示例,以使用 BluetoothChatService 类。使用 Environment.getExternalStorageDirectory 并使用 filewriter 保存到 sdcard 时没有问题,但使用带有 MODE_PRIVATE 的 openFileOutput 方法会返回 nullPointerException,如其他几个问题/答案中提到的。我一生都无法找出获得正确上下文的正确方法。
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
private Context mContext;
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");
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Save to file
String FILENAME = "chat.txt";
FileOutputStream fos = mContext.openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(bytes);
fos.close();
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
I am trying to adapt the Android BluetoothChat example to save the InputStream to file using openFileOutput from within the BluetoothChatService class. When using the Environment.getExternalStorageDirectory and saving to the sdcard using filewriter there is no problem, but using the openFileOutput method with MODE_PRIVATE returns a nullPointerException as mentioned in several other questions/answers. I cant for the life of me figure out the correct way to get the correct context.
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
private Context mContext;
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");
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Save to file
String FILENAME = "chat.txt";
FileOutputStream fos = mContext.openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(bytes);
fos.close();
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,现在 mContext 为空。您没有在任何地方为其分配任何内容。您可以更改构造函数签名以将其传入:
如果您实际上遵循该示例,则可以在
BluetoothChatService
中执行相同类型的操作...请注意,它们不会执行任何操作传入的上下文。将该上下文存储在一个字段中,ConnectedThread
也可以访问它。Well, right now mContext is null. You're not assigning anything to it anywhere. You could change your constructor signature to pass it in:
If you're actually following the example, you could do the same type of thing in the
BluetoothChatService
... note that they don't do anything with the context passed in. Store that context off in a field and it would also be accessible to theConnectedThread
.