2013-04-28 42 views
1

我使用asyncTask通过套接字将图像从Android发送到PC。Android:AsyncTask byte []问题

我打电话像这样

new SendImage().execute(data); 

,其中数据类型byte[]

和我的代码是

private class SendImage extends AsyncTask<byte[],Void, Void> { 

    @Override 
    protected Void doInBackground(byte[] ... data) { 

     try{ 


      final DataOutputStream dataOutputStream; 

      final BufferedOutputStream out = new BufferedOutputStream(RRAWsecurity.socket.getOutputStream()); 
      int count = data.length; 

      dataOutputStream = new DataOutputStream(RRAWsecurity.socket.getOutputStream()); 
      dataOutputStream.writeInt(count); 
      dataOutputStream.flush(); 

      out.write(data, 0, count); 

      out.flush(); 

     }catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 

     return null; 

} 
} 

问题是这一行

out.write(data, 0, count); 

错误说

The method write(byte[], int, int) in the type BufferedOutputStream is not applicable for the arguments (byte[][], int, int) 

我想不通为什么它要求2D阵列?

回答

3

使用data[0]而不是data。符号...只是给定类型数组的一些语法糖。所以int...实际上是一个整数的数组,你的byte...被当作一个byte []数组的数组,所以它实际上是byte[][]

+0

+1或以下是不同'doInBackground()s'的示例http://www.mcs.csueastbay.edu/~grewe/Book/Android/Source%20Code/Sockets/src/net/learn2develop/Sockets/ SocketsActivity.java – 2013-04-28 19:51:25

1

替换:

int count =data.length; to int count =data[0].length; 
out.write(data,0,count); to out.write(data[0],0,count); 

数据是字节[] []。 byte [] ...数据与byte [] []数据相同。

0

您的字节数组(byte[])是一个二维参数,但BufferedOutputStream的参数byte[][])是两维数组。关于阵列的不同尺寸是一个很大的问题。您必须将您的数组byte转换为两个二维数组。