[英]FATAL EXCEPTION: AsyncTask #1 java.lang.RuntimeException: An error occured while executing doInBackground(), while sending an image to the Server
Guys I am developing an application on android which will transfer an image from android application to the server. 伙计们,我在android上开发一个应用程序,它将图像从android应用程序传输到服务器。 I have implemented the server in JAVA and so far the application is only for the internal network purpose.
我已经用JAVA实现了服务器,到目前为止,该应用程序仅用于内部网络。 I am having an issue of FATAL Exception in my code of ASYNC class and I am not getting what I have done wrong.
我在ASYNC类的代码中遇到了致命异常的问题,但我没有做错什么。 Kindly help me out plz I will be very thankful to you people.
请帮助我,我将非常感谢你们。
Android Application Code Android应用程序代码
public class TakeImage extends ActionBarActivity {
Socket client;
DatagramSocket clientSocket;
byte[] image;
byte[] img;
private Bitmap bitmap;
private ImageButton camera;
private ImageButton gallery;
ImageView targetImage;
NotificationManager nm;
Uri fileUri = null;
private Button process;
private static int TAKE_PICTURE = 1;
private static int FROM_GALLERY = 2;
public static final int MEDIA_TYPE_IMAGE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_take_image);
//Notification Code
nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//
targetImage=(ImageView)findViewById(R.id.setImage);
process=(Button)findViewById(R.id.process);
process.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//bitmap=BitmapFactory.decodeResource(getResources(), R.id.setImage);
BitmapDrawable bd=(BitmapDrawable)targetImage.getDrawable();
bitmap=bd.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
image = stream.toByteArray();
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
Toast.makeText(getApplicationContext(),"Processing",Toast.LENGTH_LONG).show();
}
});
}
private class SendMessage extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
int c=0;
InetAddress inet=InetAddress.getByName("192.168.1.4");
client = new Socket("192.168.1.4", 4000); // connect to the server
for(int i=0;i<image.length;i++) {
img[c] = image[i];
c++;
DatagramPacket packet;
packet = new DatagramPacket(img,img.length,inet,4000);
img = new byte[1024];
c = 0;
clientSocket.send(packet);
System.out.println("sent a mini-packet");
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
Server Side Code 服务器端代码
public class Server {
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStreamReader inputStreamReader;
private static BufferedReader bufferedReader;
private static String message;
public static void main(String[] args) throws UnknownHostException {
try {
serverSocket = new ServerSocket(4000); // Server socket
} catch (IOException e) {
System.out.println("Could not listen on port: 4444");
}
System.out.println("Server started. Listening to the port 4444");
while (true) {
try {
clientSocket = serverSocket.accept(); // accept the client connection
inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); // get the client message
message = bufferedReader.readLine();
System.out.println(message);
inputStreamReader.close();
clientSocket.close();
} catch (IOException ex) {
System.out.println("Problem in message reading");
}
}
}
I have only implemented the server for just recieving the bytes not for converting it to Image. 我仅将服务器实现为仅接收字节,而不是将其转换为Image。
Log Cat 原木猫
04-04 07:13:27.630 920-981/com.example.ali.cottondiseasedetection E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException
at com.example.ali.cottondiseasedetection.TakeImage$SendMessage.doInBackground(TakeImage.java:151)
at com.example.ali.cottondiseasedetection.TakeImage$SendMessage.doInBackground(TakeImage.java:135)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
You need to initialize the array img before you assign anything to it. 您需要先对数组img进行初始化,然后再为其分配任何内容。
img = new byte[1024];
for(int i=0;i<image.length;i++) {
img[c] = image[i];
c++;
DatagramPacket packet;
packet = new DatagramPacket(img,img.length,inet,4000);
img = new byte[1024];
c = 0;
clientSocket.send(packet);
System.out.println("sent a mini-packet");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.