我想将位图发送到SocketServer但是当我点击按钮时我的应用程序崩溃了。
当我尝试发送位图时,这是代码的一部分。
button1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
MyClientTask myClientTask = new MyClientTask("192.168.1.4", 8080);
myClientTask.execute();
}
});
} 公共类MyClientTask扩展了AsyncTask {
String dstAddress="192.168.1.4";
int dstPort=8080;
String response = "";
Bitmap bm=image.getDrawingCache();
MyClientTask(String addr, int port){
dstAddress = addr;
dstPort = port;
}
@Override
protected Void doInBackground(Void... arg0) {
Socket socket = null;
try {
socket = new Socket(dstAddress, dstPort);
ByteArrayOutputStream byteArrayOutputStream =
new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 20,
stream);
byte[] byteArray = stream.toByteArray();
OutputStream os = socket.getOutputStream();
os.write(byteArray,0,byteArray.length);
os.flush();
int bytesRead;
InputStream inputStream = socket.getInputStream();
/*
* notice:
* inputStream.read() will block if no data return
*/
while ((bytesRead = inputStream.read(buffer)) != -1){
byteArrayOutputStream.write(buffer, 0, bytesRead);
response += byteArrayOutputStream.toString("UTF-8");
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "UnknownHostException: " + e.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "IOException: " + e.toString();
}finally{
if(socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
}
}
这就是我在LogCat中获得的东西,而且我并不认真对待。
03-11 22:14:10.580:E / AndroidRuntime(15582):FATAL EXCEPTION:AsyncTask#1 03-11 22:14:10.580:E / AndroidRuntime(15582):java.lang.RuntimeException:发生错误执行doInBackground时()03-11 22:14:10.580:E / AndroidRuntime(15582):在android.os.AsyncTask $ 3.done(AsyncTask.java:200)03-11 22:14:10.580:E / AndroidRuntime( 15582):at java.util.concurrent.FutureTask $ Sync.innerSetException(FutureTask.java:274)03-11 22:14:10.580:E / AndroidRuntime(15582):at java.util.concurrent.FutureTask.setException(FutureTask .java:125)03-11 22:14:10.580:E / AndroidRuntime(15582):at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:308)03-11 22:14:10.580:E / AndroidRuntime(15582):at java.util.concurrent.FutureTask.run(FutureTask.java:138)03-11 22:14:10.580:E / AndroidRuntime(15582):at java.util.concurrent.ThreadPoolExecutor.runWorker( ThreadPoolExecutor.java:1088)03-11 22:14:10.580:E / AndroidRuntime(15582):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor) .java:581)03-11 22:14:10.580:E / AndroidRuntime(15582):at java.lang.Thread.run(Thread.java:1019)03-11 22:14:10.580:E / AndroidRuntime(15582 ):引起:java.lang.NullPointerException 03-11 22:14:10.580:E / AndroidRuntime(15582):at com.example.oac.MainActivity $ MyClientTask.doInBackground(MainActivity.java:96)03-11 22: 14:10.580:E / AndroidRuntime(15582):at com.example.oac.MainActivity $ MyClientTask.doInBackground(MainActivity.java:1)03-11 22:14:10.580:E / AndroidRuntime(15582):at android.os .AsyncTask $ 2.call(AsyncTask.java:185)03-11 22:14:10.580:E / AndroidRuntime(15582):at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:306)