无法将字节数组转换为socket发送的imageView

时间:2017-01-19 00:08:44

标签: java android sockets android-imageview android-bitmap

我有问题将字节数组转换为图像,但它在 ImageView 中。

我推送后,应用程序关闭,发出错误,你会在那里看到它。 所以我需要帮助。

我尝试将图像从java服务器发送到android客户端 我已检查收到的字节数组是否已满,但我真的无法转换它。

这是我的代码:

Android客户端

package com.example.metalheadi.convert2;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {
    EditText IP, Port, Msg;
    Button Send;
    ImageView image;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Send = (Button) findViewById(R.id.send);
        IP = (EditText) findViewById(R.id.ip_e);
        Port = (EditText) findViewById(R.id.port_e);
        image = (ImageView) findViewById(R.id.imageView1);
//        Msg = (EditText) findViewById(R.id.msg_e);
        Send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Socket socket = new Socket(IP.getText().toString(), Integer.parseInt(Port.getText().toString()));

                            DataInputStream in= new DataInputStream(socket.getInputStream()) ;
                            ByteArrayOutputStream out = new ByteArrayOutputStream();
                            byte buffer[] = new byte[1024];
                            try {
                                int count;
                                while ((count = in.read(buffer)) > 0)
                                {
                                    out.write(buffer, 0, count);
                                }
                            } catch (IOException e1) {
                                e1.printStackTrace();
                            }
                            byte[]result= out.toByteArray();
                            for (int g=0;g<result.length;g++){
                                System.out.println(g);
                            }
                            System.out.println("here ");
                            Bitmap bmp = BitmapFactory.decodeByteArray(result, 0, result.length);
                            Drawable drawable = new BitmapDrawable(getResources(), bmp);
                            image.setImageDrawable(drawable);

//                            image.setImageBitmap(Bitmap.createScaledBitmap(bmp, image.getWidth(), image.getHeight(), false));

//                            DataInputStream dIn=new DataInputStream(socket.getInputStream());
//                            int length = dIn.readInt();                    // read length of incoming message
//                            if(length>0) {
//                                byte[] message = new byte[length];
//                                dIn.readFully(message, 0, message.length); // read the message
//                            }
////                            Toast.makeText(MainActivity.this,"heeeeere",Toast.LENGTH_LONG).show();
//                            System.out.println("here ");


/////////////////////////////////////////////////////////
                            socket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
                thread.start();
            }
        });
    }
}

XML android客户端

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.metalheadi.convert2.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="IP"
        android:id="@+id/ip"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="PORT"
        android:id="@+id/port"
        android:layout_below="@+id/ip_e"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ip_e"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/ip"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/port_e"
        android:layout_below="@+id/port"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/ip_e"
        android:layout_alignEnd="@+id/ip_e" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SEND"
        android:id="@+id/send"
        android:layout_below="@+id/port_e"
        android:layout_alignRight="@+id/port_e"
        android:layout_alignEnd="@+id/port_e" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView1"

        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/send"
        android:layout_alignRight="@+id/send"
        android:layout_alignEnd="@+id/send"
        android:nestedScrollingEnabled="false" />
</RelativeLayout>

服务器JAVA

package convert;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.ServerSocket;
import java.net.Socket;

import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
public class Convert {

    /**
     * @param args the command line arguments
     */
   public static void main(String[] args) throws FileNotFoundException, IOException {
        /*
         * 1. How to convert an image file to  byte array?
         */
// RandomAccessFile file1 = new RandomAccessFile("D:\\", "rw");
Thread t= new Thread(new Runnable() {
    @Override
    public void run() {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
try{
ServerSocket ss=new ServerSocket(400);
   while(true){
        File file = new File("D:\\PIC\\all pic\\han.jpg");

        FileInputStream fis = new FileInputStream(file);
        //create FileInputStream which obtains input bytes from a file in a file system
        //FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        try {
            for (int readNum; (readNum = fis.read(buf)) != -1;) {
                //Writes to this byte array output stream
                bos.write(buf, 0, readNum); 
              //  System.out.println("read " + readNum + " bytes,");
            }
        } catch (IOException ex) {
            Logger.getLogger(Convert.class.getName()).log(Level.SEVERE, null, ex);
        }
        byte[] bytes = bos.toByteArray();
          Socket s=ss.accept();
          System.out.println("Accept........");
          DataOutputStream dOut = new DataOutputStream(s.getOutputStream());
          dOut.writeInt(bytes.length); // write length of the message
          dOut.write(bytes);           // write the message              
          dOut.close();
          s.close();
 }
}
catch (IOException ex) {
                    Logger.getLogger(Convert.class.getName()).log(Level.SEVERE, null, ex);
                }

    }
});
   t.start();
}

}

Android logcat错误

1-19 01:43:43.964 24869-25529/com.example.metalheadi.convert2 E/AndroidRuntime: FATAL EXCEPTION: Thread-11797

android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触及其视图。 在android.view.ViewRootImpl.checkThread(ViewRootImpl.java:5498) 在android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1062) 在android.view.View.requestLayout(View.java:16375) 在android.view.View.requestLayout(View.java:16375) 在android.view.View.requestLayout(View.java:16375) 在android.view.View.requestLayout(View.java:16375) 在android.view.View.requestLayout(View.java:16375) 在android.view.View.requestLayout(View.java:16375) 在android.widget.RelativeLayout.requestLayout(RelativeLayout.java:318) 在android.view.View.requestLayout(View.java:16375) 在android.widget.ImageView.setImageDrawable(ImageView.java:418)at com.example.metalheadi.convert2.MainActivity $ 1 $ 1.run(MainActivity.java:66) 在java.lang.Thread.run(Thread.java:856)

3 个答案:

答案 0 :(得分:0)

该异常显示{0}未在UI线程上运行。在Android中,所有UI操作必须在主线程上执行。您可以将呼叫包含在下面:

setImageDrawable

答案 1 :(得分:0)

由于您要跨越一个新线程来读取套接字,因此应该从ui线程调用image.setImageDrawable(drawable);

您可以将其替换为:

 runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        image.setImageDrawable(drawable);
                    }
                });

然而,使用AsyncTask将是一个更好的解决方案,而不是跨越自己的线程,因为它有更方便的回调方法来切换ui和后台线程。

答案 2 :(得分:0)

您正在将图像尺寸写在图像之前,但是您没有阅读它:相反,您将长度字视为图像的一部分。