Android - send image through bluetooth programmatically

10,266

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

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

Additional Help:

Share:
10,266

Related videos on Youtube

Cœur
Author by

Cœur

Everybody should contribute to clean up Stack Overflow. SO is intended to be a top-quality Q&A site, meant not just for the OP, but for posterity. Thanks to search engines, questions and answers become authoritative for the whole Internet. --Paul Draper TODO: disambiguate the 18,300+ duplicate titles from 41,600+ questions fix the uneditable titles (1,117 titles with length < 15) fix the uneditable titles (containing "help", "problem", "question", "doubt", …) fix the uneditable messages (containing "mydomain.com", "domain.com", "mysite.com", "site.com", "abc.com", "xyz.com", …) fix the uneditable messages with link shorteners (5,032 url:goo.gl, 3,673 url:bit.ly, 1,982 url:tinyurl.com, 1,748 url:cl.ly, …) remove the dead images/codes (8,051 url:imageshack.us, 2,818 url:pastie.org, 2,307 url:photobucket, 430 url:skitch.com, 214 url:rapidshare.com, 78 url:paste.ofcode.org, 58 url:expirebox.com, 4 url:megaupload.com, …) fix the broken links in messages, broken links to connect.microsoft.com, … review the potentially broken Apple links: #DOCUMENTATION in the URL, /library but not /archive in the URL, url:developer.apple.com/mac/library, url:developer.apple.com/safari/library rollback the 99+ solved, resolved, fixed, answered titles (meta, alternative query) correct the spelling in titles correct the 6,600+ "thanks in advanced" and 1,100+ "thanks in advice", …

Updated on September 18, 2022

Comments

  • Cœur
    Cœur over 1 year

    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.

    This sends the picture:

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

    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;
                }
            }
        }
    

    This reads the data back in:

        case MESSAGE_READ:
        byte[] readBuf = (byte[]) msg.obj;
        Bitmap bmp = BitmapFactory.decodeByteArray(readBuf, 0, msg.arg1);
    
  • Admin
    Admin about 11 years
    The idea is to control another devices camera (which is implemented), then send the captured image across. So ideally there is no human interaction with the camera device (only to set up the application).
  • syb0rg
    syb0rg about 11 years
    See the links provided for more help on your issue.