普通网友 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
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog