无法通过蓝牙传输传输图像android

发布于 2025-01-04 01:37:30 字数 5836 浏览 7 评论 0原文

我正在 Android 上开发一个共享联系信息+图片的应用程序 我有包含完整信息的类内容。

import java.io.Serializable;

public class Stuff implements Serializable {

    private String name;
    private String company;
    private String title;
    private String address;
    private String phone;
    private String cel;
    private String email;
    private int template;
    private boolean isPopulate;
    private byte[] picture;

    public Stuff(String n, String c, String t, String a, String ph, String cl,
            String e, int tmp, byte[] pic, boolean check) {
        this.name = n;
        this.company = c;
        this.title = t;
        this.address = a;
        this.phone = ph;
        this.cel = cl;
        this.email = e;
        this.template = tmp;
        this.isPopulate = check;
        this.picture = pic;
    }

    public String getName() {
        return this.name;
    }

    public String getCompany() {
        return this.company;
    }

    public String getTitle() {
        return this.title;
    }

    public String getAddress() {
        return this.address;
    }

    public String getPhone() {
        return this.phone;
    }

    public String getCel() {
        return this.cel;
    }

    public String getEmail() {
        return this.email;
    }

    public int getTemplate() {
        return this.template;
    }

    public byte[] getPicture() {
        return this.picture;
    }

    public boolean getCheck() {
        return this.isPopulate;
    }
}

这是我通过套接字传输图像的线程

private class ConnectedThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream = null;
    private final OutputStream mmOutStream = null;
    private ObjectOutputStream oos = null;
    private ObjectInputStream ois = null;

    public ConnectedThread(BluetoothSocket socket) {
        Log.d(TAG, "create ConnectedThread");
        mmSocket = socket;
        Log.d("connected thread ", "connected thread constructor inside");
    }

    public void run() {
        Log.i(TAG, "BEGIN mConnectedThread");

        // Keep listening to the InputStream while connected

        try {
            Log.d("Connected thread run ", "start while");

            switch (ROLE) {

            case 1:
                // read from client
                Log.d("Connected thread 1 ", "ois before");
                ObjectInputStream tmpIn1 = new ObjectInputStream(
                        new BufferedInputStream(mmSocket.getInputStream()));
                Log.d("Connected thread 1  ", "ois after");
                try {
                    Stuff obj_rcv = (Stuff) tmpIn1.readObject();
                    Log.d("Connected thread 1  ", "after read");
                    Message msg2 = mHandler
                            .obtainMessage(RemoteBusinessCard.MESSAGE_READ);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("person", obj_rcv);
                    msg2.setData(bundle);
                    mHandler.sendMessage(msg2);

                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                // write to client

                ObjectOutputStream tmpOut1 = new ObjectOutputStream(
                        new BufferedOutputStream(mmSocket.getOutputStream()));
                tmpOut1.writeObject(person);
                tmpOut1.flush();
                Log.d("Connected thread 1  ", "oos created");
                tmpIn1.close();
                tmpOut1.close();

                break;
            case 2:
                // write to server

                ObjectOutputStream tmpOut2 = new ObjectOutputStream(
                        new BufferedOutputStream(mmSocket.getOutputStream()));
                tmpOut2.writeObject(person);
                tmpOut2.flush();
                Log.d("Connected thread 2  ", "oos created");
                // read from server
                ObjectInputStream tmpIn2 = new ObjectInputStream(
                        new BufferedInputStream(mmSocket.getInputStream()));
                Log.d("Connected thread 2  ", "ois created");
                try {
                    Stuff obj_rcv = (Stuff) tmpIn2.readObject();
                    Message msg2 = mHandler
                            .obtainMessage(RemoteBusinessCard.MESSAGE_READ);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("person", obj_rcv);
                    msg2.setData(bundle);
                    mHandler.sendMessage(msg2);
                    Log.d("Connected thread 2  ", "send message");
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                tmpOut2.close();
                tmpIn2.close();

                break;
            }
        } catch (IOException e) {
            Log.e(TAG, "disconnected", e);
            connectionLost();
        }
    }

    /**
     * Write to the connected OutStream.
     * 
     * @param buffer
     *            The bytes to write
     */
    public void write(Stuff object) {
        try {
            Log.d("BTS", "inside write before" + object.getName());
            oos.writeObject(object);
            Log.d("BTS", "inside write after" + object.getName());
            oos.flush();
            oos.close();

        } catch (IOException e) {
            Log.e(TAG, "Exception during write", e);
        }
    }

    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
            Log.e(TAG, "close() of connect socket failed", e);
        }
    }
}

现在图像没有在第二个设备上传输,导致问题,甚至数据也没有。问题出在哪里。

I am developing an application on android that share contact info + picture
I have class Stuff that contains whole info.

import java.io.Serializable;

public class Stuff implements Serializable {

    private String name;
    private String company;
    private String title;
    private String address;
    private String phone;
    private String cel;
    private String email;
    private int template;
    private boolean isPopulate;
    private byte[] picture;

    public Stuff(String n, String c, String t, String a, String ph, String cl,
            String e, int tmp, byte[] pic, boolean check) {
        this.name = n;
        this.company = c;
        this.title = t;
        this.address = a;
        this.phone = ph;
        this.cel = cl;
        this.email = e;
        this.template = tmp;
        this.isPopulate = check;
        this.picture = pic;
    }

    public String getName() {
        return this.name;
    }

    public String getCompany() {
        return this.company;
    }

    public String getTitle() {
        return this.title;
    }

    public String getAddress() {
        return this.address;
    }

    public String getPhone() {
        return this.phone;
    }

    public String getCel() {
        return this.cel;
    }

    public String getEmail() {
        return this.email;
    }

    public int getTemplate() {
        return this.template;
    }

    public byte[] getPicture() {
        return this.picture;
    }

    public boolean getCheck() {
        return this.isPopulate;
    }
}

here is the thread through which i am transferring image via socket

private class ConnectedThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream = null;
    private final OutputStream mmOutStream = null;
    private ObjectOutputStream oos = null;
    private ObjectInputStream ois = null;

    public ConnectedThread(BluetoothSocket socket) {
        Log.d(TAG, "create ConnectedThread");
        mmSocket = socket;
        Log.d("connected thread ", "connected thread constructor inside");
    }

    public void run() {
        Log.i(TAG, "BEGIN mConnectedThread");

        // Keep listening to the InputStream while connected

        try {
            Log.d("Connected thread run ", "start while");

            switch (ROLE) {

            case 1:
                // read from client
                Log.d("Connected thread 1 ", "ois before");
                ObjectInputStream tmpIn1 = new ObjectInputStream(
                        new BufferedInputStream(mmSocket.getInputStream()));
                Log.d("Connected thread 1  ", "ois after");
                try {
                    Stuff obj_rcv = (Stuff) tmpIn1.readObject();
                    Log.d("Connected thread 1  ", "after read");
                    Message msg2 = mHandler
                            .obtainMessage(RemoteBusinessCard.MESSAGE_READ);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("person", obj_rcv);
                    msg2.setData(bundle);
                    mHandler.sendMessage(msg2);

                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                // write to client

                ObjectOutputStream tmpOut1 = new ObjectOutputStream(
                        new BufferedOutputStream(mmSocket.getOutputStream()));
                tmpOut1.writeObject(person);
                tmpOut1.flush();
                Log.d("Connected thread 1  ", "oos created");
                tmpIn1.close();
                tmpOut1.close();

                break;
            case 2:
                // write to server

                ObjectOutputStream tmpOut2 = new ObjectOutputStream(
                        new BufferedOutputStream(mmSocket.getOutputStream()));
                tmpOut2.writeObject(person);
                tmpOut2.flush();
                Log.d("Connected thread 2  ", "oos created");
                // read from server
                ObjectInputStream tmpIn2 = new ObjectInputStream(
                        new BufferedInputStream(mmSocket.getInputStream()));
                Log.d("Connected thread 2  ", "ois created");
                try {
                    Stuff obj_rcv = (Stuff) tmpIn2.readObject();
                    Message msg2 = mHandler
                            .obtainMessage(RemoteBusinessCard.MESSAGE_READ);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("person", obj_rcv);
                    msg2.setData(bundle);
                    mHandler.sendMessage(msg2);
                    Log.d("Connected thread 2  ", "send message");
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                tmpOut2.close();
                tmpIn2.close();

                break;
            }
        } catch (IOException e) {
            Log.e(TAG, "disconnected", e);
            connectionLost();
        }
    }

    /**
     * Write to the connected OutStream.
     * 
     * @param buffer
     *            The bytes to write
     */
    public void write(Stuff object) {
        try {
            Log.d("BTS", "inside write before" + object.getName());
            oos.writeObject(object);
            Log.d("BTS", "inside write after" + object.getName());
            oos.flush();
            oos.close();

        } catch (IOException e) {
            Log.e(TAG, "Exception during write", e);
        }
    }

    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
            Log.e(TAG, "close() of connect socket failed", e);
        }
    }
}

Now image is not transferred on second device causing problem, not even data . Where is the problem .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文