2

Good day, i have a requirement where i need to send files(image/videos.. etc) through a bluetooth socket connection. Am using the Bluetooth chat example in the SDK as a guideline. I can connect successfully, but am having trouble converting the files in the sdcard to byte array so i can write it via an outputStream.

i can convert images using the following code:

//After getting the imageId from the cursor object
 Bitmap bitmap = Media.getBitmap(getContentResolver(), imageUri);
ByteArrayOutputStream baos = new ByteArrayOutputStream();

bitmap.compress(Bitmap.CompressFormat.JPEG, 90, baos);
bytes[] bytes = baos.toByteArray();

but having trouble converting videos or other files, should i be using a ObjectOutputStream for all conversion or is there another way i can do this because i can't seem to convert a fileOutputStream to a byte Array? Thank you

Michał Kuliński
  • 1,928
  • 3
  • 27
  • 51
irobotxx
  • 5,963
  • 11
  • 62
  • 91

5 Answers5

4

use getContentResolver().openInputStream(uri) to get an InputStream from a URI. and then read the data from inputstream convert the data into byte[] from that inputstream

Try with following code

public byte[] readBytes(Uri uri) throws IOException {
          // this dynamically extends to take the bytes you read
        InputStream inputStream = getContentResolver().openInputStream(uri);
          ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();

          // this is storage overwritten on each iteration with bytes
          int bufferSize = 1024;
          byte[] buffer = new byte[bufferSize];

          // we need to know how may bytes were read to write them to the byteBuffer
          int len = 0;
          while ((len = inputStream.read(buffer)) != -1) {
            byteBuffer.write(buffer, 0, len);
          }

          // and then we can return your byte array.
          return byteBuffer.toByteArray();
        }
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
  • Thanks, should have thought of that, would give it a go. – irobotxx Dec 23 '11 at 10:23
  • 2
    Note that this may not be a good idea. A video file may be very large, but heap memory allocation is limited to 16 MB usually (for the entire application). Reading a large file this way is likely to result in `OutOfMemoryError`. – Aleks G Dec 23 '11 at 10:25
  • @Alkesh G : i have used the same thing in my app for upload the video. it cause the outOfmemory while returning the bytes. What will be the solution. – Arpit Patel Mar 06 '13 at 04:25
2

A video usually is big, so storing it entirely in memory may not be possible. This means you shouldn't convert a video to a giant byte[]. Instead you need a fixed size byte[] that will act as a buffer. You will load piece by piece of the video file into that buffer and send it over and over again, with different data.

Thiago Chaves
  • 9,218
  • 5
  • 28
  • 25
1

To expand on the previous two answers, you may end up with something like this:

void sendFile(Uri uri, BluetoothSocket bs) throws IOException
{
    try
    {
        BufferedInputStream bis = new BufferedInputStream(getContentResolver().openInputStream(uri));
        OutputStream os = bs.getOutputStream();
        int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];

        // we need to know how may bytes were read to write them to the byteBuffer
        int len = 0;
        while ((len = inputStream.read(buffer)) != -1)
        {
            os.write(buffer, 0, len);
        }
    }
    finally
    {
        if(bis != null)
            bis.close();
    }
}

You would call this method passing a Uri of the file and the BluetoothSocket to which your file needs to be sent. This method would then read the given file and send it to the specified socket. It would throw an IOException if an error occurs during communication.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • sorry for the late response. Please have you been successfully in sending files using the outputStream. I have tried sending files to my pc or another android device (rooted g1 with 2.1) and have had no success at all. please any advice on how to do this?.. the write method in that example is taking forever and nothing happens.. – irobotxx Dec 23 '11 at 17:21
  • I haven't really tested this, as I didn't have an appropriate setup. I have used a similar routine to send file over network socket successfully, which shouldn't be much different. Make sure your socket is connected and the receiving side authorised incoming transfer. – Aleks G Dec 23 '11 at 19:02
  • i have done that but nothing happens. i was wondering if i have to code the receiving device to receive the bytes but that would be silly i think. i started a this post to explain my troubles http://stackoverflow.com/questions/8619119/send-files-via-bluetooth-socket-based-on-bluetoothchat-example – irobotxx Dec 23 '11 at 20:01
0

This may be a late reply, but I recently tried to do the same thing and ended up with out of memory errors. My alternative was to read the file frame by frame(using the Bitmap class). Then I converted each frame to byte array. This allowed me to send the file frame by frame

Rhycce
  • 111
  • 1
  • 8
0

To smth like this:

   InputStream in = null;
   try {
     in = new BufferedInputStream(new FileInputStream(file));

    finally {
     if (in != null) {
       in.close();
     }
   }

And then use approach proposed here: http://www.java2s.com/Code/Android/File/InputStreamtobytearraycopyReaderandWriter.htm

Yury
  • 20,618
  • 7
  • 58
  • 86