简体   繁体   中英

How to convert files to byte[] Array to send via Bluetooth socket in Android?

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

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

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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM