普通网友 2015-03-28 11:44 采纳率: 15.8%
浏览 1852

android客户端接收服务器端发来的图片异常

threadid=11: thread exiting with uncaught exception (group=0x40a13300)
FATAL EXCEPTION: Thread-78
java.lang.NegativeArraySizeException: -2555936
at me.demo.transimage.MainActivity$1$1.run(MainActivity.java:59)
客户端代码
public class MainActivity extends Activity
{

private ImageView imageView;
private Bitmap bitmap;
private Button button;
public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

    imageView = (ImageView) findViewById(R.id.imageView1);  
    button = (Button) findViewById(R.id.button1);  
    button.setOnClickListener(new OnClickListener() 
    {  
        public void onClick(View v) 
        {  
                new Thread()
                {
                            public void run()
                            {
                                            Socket socket = null;  
                                            byte[] data = null;
                                            try {  
                                                socket = new Socket("10.0.2.2",1346);  
                                                DataInputStream dis = new DataInputStream(socket.getInputStream());  


                                                if( dis.available()>=0)
                                                {
                                                    data = new byte[dis.readInt()];  
                                                    bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                                                    imageView.setImageBitmap(bitmap); 
                                                }
                                            } 
                                            catch (IOException e) 
                                            {  
                                                e.printStackTrace();  
                                            } 
                                            finally 
                                            {  
                                                try 
                                                {  
                                                    if(socket!=null)
                                                    socket.close();  
                                                } 
                                                catch (IOException e) 
                                                {  
                                                    e.printStackTrace();  
                                                }  
                                            }  

                            }                       

                }.start();        
        }  
    });
}  

}

服务器端代码
public class TransImageServer
{

public static void main(String[] args)
{

try
{

ServerSocket server = new ServerSocket(1346);

System.out.println("服务器已启动,正在监听1346端口 ");

                Socket socket = server.accept();  

                DataOutputStream dos = new DataOutputStream(socket.getOutputStream());  
                FileInputStream fis = new FileInputStream("F:\\project_test_image\\avatar3_2.jpg");  
                byte[] data = new byte[fis.available()];
                System.out.println("fis.available          " + (fis.available()));
                fis.read(data);  
                dos.write(data);  
                dos.flush();  
                dos.close();  
                fis.close();  
                socket.close();  
                server.close();  

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

}

服务器端 System.out.println("fis.available " + (fis.available()));
输出为 服务器已启动,正在监听1346端口
fis.available 1031
但是客户端却报出有异常,java.lang.NegativeArraySizeException: -2555936

  • 写回答

1条回答 默认 最新

  • 泽济天下 2015-03-29 04:30
    关注

    图片说明
    不知道你的问题解决了没有

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!