Search code examples
azureandroid-4.4-kitkatazure-iot-hub

Calling Azure IoT File Upload from Android KitKat is returning BAD_FORMAT


I am trying to upload a .png file to Azure IoT Hubs, but for some reason I am constantly getting BAD_FORMAT. I am using com.microsoft.azure.sdk.iot:iot-device-client:1.14.2 library as I need to using an android device that is quite old (KitKat version).

The code I am using:

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

I have the file payments.json in the device (emulator).

Any help would be appreciated!


Solution

  • It seems I just needed to import a separate library for accessing blob storage. Had to add the following to my gradle script:

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