从 Android KitKat 调用 Azure IoT 文件上传正在返回 BAD_FORMAT

Calling Azure IoT File Upload from Android KitKat is returning BAD_FORMAT

我正在尝试将 .png 文件上传到 Azure IoT 中心,但由于某种原因,我不断收到 BAD_FORMAT。我正在使用 com.microsoft.azure.sdk.iot:iot-device-client:1.14.2 库,因为我需要使用相当旧的 android 设备(KitKat 版本)。

我使用的代码:

    public void btnFileUploadOnClick(View v) throws URISyntaxException, IOException
    {
        Log.i("IoT App","Uploading file to IoT Hub...");

        EditText text = (EditText)findViewById(R.id.editTextFileName);
        String fullFileName = text.getText().toString();

        try
        {
            File directory = Environment.getExternalStorageDirectory();
            File file = new File(directory, "payments.json");

            InputStream inputStream = new FileInputStream(file);
            long streamLength = file.length();

            if(file.isDirectory())
            {
                throw new IllegalArgumentException(fullFileName + " is a directory, please provide a single file name, or use the FileUploadSample to upload directories.");
            }
            else
            {
                client.uploadToBlobAsync("payments", inputStream, streamLength, new FileUploadStatusCallBack(), null);
            }

            Log.i("IoT App","File upload started with success");
            Log.i("IoT App","Waiting for file upload callback with the status...");
        }
        catch (Exception e)
        {
            Log.e("IoT App","Exception while sending event: " + e.getMessage());
        }
    }

    protected class FileUploadStatusCallBack implements IotHubEventCallback
    {
        public void execute(IotHubStatusCode status, Object context)
        {
            Log.i("IoT App","IoT Hub responded to file upload operation with status " + status.name());
            TextViewControl.log("IoT Hub responded to file upload operation with status " + status.name());
        }
    }

我在设备(模拟器)中有文件 payments.json

如有任何帮助,我们将不胜感激!

看来我只需要导入一个单独的库来访问 blob 存储。必须将以下内容添加到我的 gradle 脚本中:

implementation 'com.microsoft.azure.android:azure-storage-android:2.0.0'