Android的 - 通过蓝牙发送图片programically [英] Android - send image through bluetooth programically

查看:390
本文介绍了Android的 - 通过蓝牙发送图片programically的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经修改了Android蓝牙聊天示例应用程序,现在整个发送图像。这是罚款的第一张图像。它得到跨越和显示正确发送。当我尝试发送另一个形象,似乎送previous图像跨越20+次,当它应该只是把新形象了一次。我已经尝试使用持久自由行动的,但无济于事尝试。

I have modified the Android Bluetooth Chat sample application to now send images across. This is fine for the first image. It gets sent across and displays correctly. When I try to send another image, it seems to send the previous image across 20+ times, when it should just send the new image over once. I have tried using oef's but to no avail.

这将画面:

        Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.rc_a);

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        icon.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
        byte[] image = bytes.toByteArray();

        mConnection.write(image);

这是在ConnectedThread:

This is in the ConnectedThread:

    public void run() {
        byte[] buffer = new byte[1024];
        byte[] imgBuffer = new byte[1024 * 1024];
        int pos = 0;

        // Keep listening to the InputStream while connected
        while (true) {
            try {
                // Read from the InputStream
                int bytes = mmInStream.read(buffer);
                System.arraycopy(buffer,0,imgBuffer,pos,bytes);
                pos += bytes;

                mHandler.obtainMessage(BtoothSetupActivity.MESSAGE_READ,
                        pos, -1, imgBuffer).sendToTarget();

            } catch (IOException e) {
                connectionLost();
                break;
            }
        }
    }

这读取数据回:

    case MESSAGE_READ:
    byte[] readBuf = (byte[]) msg.obj;
    Bitmap bmp = BitmapFactory.decodeByteArray(readBuf, 0, msg.arg1);

任何想法?

推荐答案

有关传输文件,您可以使用意图做出 ACTION_SEND 的显式调用。

For transferring files you can make an explicit call to ACTION_SEND using intents.

使用 ACTION_SEND ,菜单将与应用程序,可以处理您要发送的文件类型,从用户需要选择蓝牙,然后在弹出其中设备发送文件。

With ACTION_SEND, a menu will popup with the application that can handle the file type you want to send, from which the user will need to select Bluetooth, and then the device of which to send the file.

File sourceFile = new File("//mnt/sdcard/file.apk"); 
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
Intent.setType("image/jpeg"); 
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(sourceFile));
startActivity(intent);

其他帮助:

  • Bluetooth file transfer Android
  • Android Bluetooth file sending

这篇关于Android的 - 通过蓝牙发送图片programically的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆