如何通过代码通过蓝牙将文件从Android设备发送到其他设备 [英] How to send file from Android device to other device through Bluetooth by code

查看:34
本文介绍了如何通过代码通过蓝牙将文件从Android设备发送到其他设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发使用蓝牙将图像/txt 或任何文件从一个 android 设备发送到另一个非 android 设备的应用程序.

I want to develop application that sends image/txt or any file from one android device to another none android device using Bluetooth.

请问有人可以为此提供帮助或源代码吗?

Please anyone can give help or source code for that?

推荐答案

这是您可以通过蓝牙将文件从安卓设备发送到任何设备的代码.

Here is the code from which you can send file via bluetooth from android device to any device.

btnOk.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                txtContent = (EditText)findViewById(R.id.txtContent);
                imageView = (ImageView)findViewById(R.id.imageView);
                linearLayout = (LinearLayout)findViewById(R.id.linearLayout);

                viewToBeConverted = (TextView) findViewById(R.id.hello);
                linearLayout.setDrawingCacheEnabled(true);

                //Toast.makeText(MainActivity.this, file.toString(), Toast.LENGTH_LONG).show();
                try
                {
                    if(file.exists())
                    {
                        file.delete();
                    }
                    out = new FileOutputStream(file);
                }
                catch (Exception e) 
                {
                    Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                }


                viewToBeConverted.setText(txtContent.getText().toString());
                viewToBeConverted.setDrawingCacheEnabled(true);

               // Toast.makeText(MainActivity.this, " " + viewToBeConverted.getDrawingCache(), Toast.LENGTH_LONG).show();
                txtContent.setText("");

                Bitmap viewBitmap = linearLayout.getDrawingCache();


                linearLayout.setVisibility(1);
                imageView.setImageBitmap(viewBitmap);

                ByteArrayOutputStream baos = new ByteArrayOutputStream();  
                viewBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object

                byte[] b = baos.toByteArray();  

                try 
                {

                    out.write(b);
                    out.flush();
                    out.close();

                    Intent intent = new Intent();  
                    intent.setAction(Intent.ACTION_SEND);  
                    intent.setType("image/png");
                    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file) );  
                    startActivity(intent);
                }
                catch (Exception e) 
                {
                    Toast.makeText(MainActivity.this, " " + e.getMessage(), Toast.LENGTH_LONG).show();

                }
            }
        });

享受.:)

这篇关于如何通过代码通过蓝牙将文件从Android设备发送到其他设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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