如何将文件转换为字节[]数组,以便在Android中通过蓝牙套接字发送?

如何将文件转换为字节[]数组,以便在Android中通过蓝牙套接字发送?,android,Android,您好,我有一个要求,我需要通过蓝牙插座连接发送文件(图像/视频等)。我使用SDK中的蓝牙聊天示例作为指导。我可以成功连接,但在将SD卡中的文件转换为字节数组以便通过输出流写入时遇到问题 我可以使用以下代码转换图像: //After getting the imageId from the cursor object Bitmap bitmap = Media.getBitmap(getContentResolver(), imageUri); ByteArrayOutputStream bao

您好,我有一个要求,我需要通过蓝牙插座连接发送文件(图像/视频等)。我使用SDK中的蓝牙聊天示例作为指导。我可以成功连接,但在将SD卡中的文件转换为字节数组以便通过输出流写入时遇到问题

我可以使用以下代码转换图像:

//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();
但在转换视频或其他文件时遇到问题,我应该使用ObjectOutputStream进行所有转换,还是有其他方法可以这样做,因为我似乎无法将fileOutputStream转换为字节数组?感谢您对smth这样说:

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

    finally {
     if (in != null) {
       in.close();
     }
   }
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();
    }
}
然后使用此处提出的方法:

视频通常很大,因此不可能完全存储在内存中。这意味着您不应该将视频转换为大字节[]。相反,您需要一个固定大小的字节[],用作缓冲区。您将视频文件逐段加载到该缓冲区,并使用不同的数据反复发送。

使用
getContentResolver().openInputStream(uri)
从uri获取InputStream。然后从inputstream读取数据,并将数据从该inputstream转换为字节[]

尝试以下代码

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

要扩展前两个答案,您可能会得到如下结果:

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

    finally {
     if (in != null) {
       in.close();
     }
   }
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();
    }
}

您可以通过传递文件的
Uri
和文件需要发送到的
BluetoothSocket
调用此方法。然后,此方法将读取给定的文件并将其发送到指定的套接字。如果在通信过程中发生错误,它将抛出一个
IOException

这可能是一个延迟回复,但我最近尝试过做同样的事情,结果导致内存不足错误。我的选择是逐帧读取文件(使用位图类)。然后我将每个帧转换为字节数组。这让我可以一帧一帧地发送文件

谢谢,我应该想到这一点,我会尝试一下。请注意,这可能不是一个好主意。视频文件可能非常大,但堆内存分配通常限制为16MB(对于整个应用程序)。以这种方式读取大文件可能会导致
OutOfMemoryError
@Alkesh G:我在应用程序中使用了相同的东西来上传视频。它会在返回字节时导致outOfmemory。解决方案是什么?很抱歉,反应太晚了。请检查您是否已成功使用outputStream发送文件。我曾尝试将文件发送到我的电脑或其他安卓设备(2.1版本的根g1),但没有成功。请告诉我怎么做?。。这个例子中的write方法花费了很长时间,什么也没有发生。我还没有真正测试它,因为我没有一个合适的设置。我使用了一个类似的例程成功地通过网络套接字发送文件,这应该不会有太大的不同。确保您的插座已连接,且接收端已授权传入传输。我已完成此操作,但未发生任何情况。我想知道我是否必须对接收设备进行编码以接收字节,但我认为这是愚蠢的。我开始写这篇文章来解释我的烦恼