Stream file from my cloud app to 3rd party app (handle read/write byte[] requests) in Android

59 views Asked by At

I want to stream my files directly from my cloud app to 3rd party apps, without downloading the file in advance on non-rooted device.

For example: When I create an Intent to open an audio file from my Cloud app with any 3rd party audio player, the player may not need to read() the whole file, but only specific range of bytes[] from the file, which are played in the specific seconds (streaming). So I want to provide a virtual file to the 3rd party player app and when the player starts reading the chunks of the file, the read(byte[], offset, count) requests to be send to my cloud app with the requested range of bytes, so my app to be able to return these chunks of bytes[] (I have the opportunity to return only specific byte range of the file from my cloud server).

I try to use DocumentProvider and FileProvider, but when openDocument(..)/openFile(..) is called from the 3rd party app, my app can return only ParcelFileDescryptor for a file, which is downloaded already. I can`t return my own FileInputStream object, from which I can return the requested bytes[] from the 3rd party app with read() requests.

Is there any way to provide (stream) a file to 3rd party app and the read(byte[], offset, count) requests to be redirected to my cloud app, and my app return the specific bytes to the 3rd party app?

My code from the openDocument() method in DocumentProvider:

@Override
public ParcelFileDescriptor openDocument(final String documentId, final String mode,
                                         CancellationSignal signal)
        throws FileNotFoundException {

    final File file = getFileForDocId(documentId);
    final int accessMode = ParcelFileDescriptor.parseMode(mode);
    final boolean isWrite = (mode.indexOf('w') != -1);

    if (isWrite) {
        // Attach a close listener if the document is opened in write mode.
        try {
            Handler handler = new Handler(getContext().getMainLooper());
            return ParcelFileDescriptor.open(file, accessMode, handler,
                    new ParcelFileDescriptor.OnCloseListener() {
                        @Override
                        public void onClose(IOException e) {
                            Log.i(TAG, "A file with id " + documentId + " has been closed!  Time to " +
                                    "update the server.");
                        }

                    });
        } catch (IOException e) {
            throw new FileNotFoundException();
        }
    } else {
        return ParcelFileDescriptor.open(file, accessMode);
    }
}
0

There are 0 answers