0

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);
    }
}
Peter Baev
  • 41
  • 4
  • `I want to stream my files directly from my cloud app` Extend ContentProvider. No intermediate storage needed. – blackapps Dec 01 '22 at 13:20
  • And read about -not possible- ramdom access in: https://stackoverflow.com/questions/9637629/can-we-install-an-apk-from-a-contentprovider. (but i did not check that) – blackapps Dec 01 '22 at 13:21
  • How can I attach to the read/write requests from the other 3rd party app if I extend ContentProvider? Can you give me an example code? – Peter Baev Dec 01 '22 at 13:42
  • Hmmm. Streaming is reading only for me. I only experimented with reading using ContentProvider for files not from local storage. – blackapps Dec 01 '22 at 13:47
  • Can you give me code example only for reading, please? – Peter Baev Dec 02 '22 at 12:17
  • When i experimented with the openFile() function extending ContentProvider i used code from https://stackoverflow.com/questions/18212152/transfer-inputstream-to-another-service-across-process-boundaries-with-parcelf but never got it really done. I ended up first downloding the file to storage and then opening it from there. Piping is the word. – blackapps Dec 02 '22 at 12:40
  • One of the problems is that receiving data from internet needs to be done in a thread. I supplied the provider with a thread and used a Thread.join() but maybe its not needed when de consumer uses a thread. (As is advised doing I/O). – blackapps Dec 02 '22 at 12:45
  • https://github.com/google/samba-documents-provider/blob/master/app/src/main/java/com/google/android/sambadocumentsprovider/provider/SambaDocumentsProvider.java – blackapps Dec 02 '22 at 12:54

0 Answers0