如何从Android设备通过蓝牙由code文件发送到另一台设备 [英] How to send file from Android device to other device through Bluetooth by code

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

问题描述

我要开发的应用程序发送的图像/ TXT或从一台Android设备通过蓝牙另一个没有的Andr​​oid设备上的任何文件。

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

请任何人都可以给予帮助或源$ C ​​$ C是什么?

Please anyone can give help or source code for that?

推荐答案

下面是code,您可以通过蓝牙从Android设备发送文件到任何设备。

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();

                }
            }
        });

享受。 :)

Enjoy. :)

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

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