2015-04-03 98 views
0

夥計們我正在開發一個應用程序在Android上將圖像從android應用程序傳輸到服務器。我已經在JAVA中實現了服務器,到目前爲止,該應用程序僅用於內部網絡目的。我在ASYNC類的代碼中遇到了致命異常的問題,我沒有得到我做錯了什麼。請幫助我,我會非常感謝你的人。致命異常:AsyncTask#1 java.lang.RuntimeException:執行doInBackground()時發生錯誤,同時發送圖像到服務器

的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; 
    } 

} 

服務器端代碼

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"); 
     } 
    } 

} 

我只執行了服務器剛剛recieving字節不是將其轉換爲圖像。

登錄貓

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) 

+1

請在logcat中發佈錯誤的確切輸出。 – Chris 2015-04-03 17:41:15

+0

@Chris現在我也根據您的要求放置了日誌貓。 – 2015-04-04 07:17:29

回答

0

需要初始化數組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"); 
} 
+0

我也試過這個,但沒有爲我工作。 – 2015-04-04 07:18:12

相關問題