TeamNewPipe/NewPipe · error · IOException

Cannot get the ParcelFileDescriptor for

Error message

Cannot get the ParcelFileDescriptor for 

What it means

Thrown by FileStreamSAF's constructor when ContentResolver.openFileDescriptor(fileUri, "rw") returns null. A null ParcelFileDescriptor means the provider could not open the document for read-write seekable access — the provider exists but refused the mode, the URI is stale, or the file is a virtual (cloud) document that cannot be opened as a raw file descriptor. The comment in-source explicitly notes read-write seek mode is required and cloud virtual files are unsupported.

Source

Thrown at app/src/main/java/us/shandian/giga/io/FileStreamSAF.java:35

public class FileStreamSAF extends SharpStream {

    private final FileInputStream in;
    private final FileOutputStream out;
    private final FileChannel channel;
    private final ParcelFileDescriptor file;

    private boolean disposed;

    public FileStreamSAF(@NonNull ContentResolver contentResolver, Uri fileUri) throws IOException {
        // Notes:
        // the file must exists first
        // ¡read-write mode must allow seek!
        // It is not guaranteed to work with files in the cloud (virtual files), tested in local storage devices

        file = contentResolver.openFileDescriptor(fileUri, "rw");

        if (file == null) {
            throw new IOException("Cannot get the ParcelFileDescriptor for " + fileUri.toString());
        }

        in = new FileInputStream(file.getFileDescriptor());
        out = new FileOutputStream(file.getFileDescriptor());
        channel = out.getChannel();// or use in.getChannel()
    }

    @Override
    public int read() throws IOException {
        return in.read();
    }

    @Override
    public int read(byte[] buffer) throws IOException {
        return in.read(buffer);
    }

    @Override

View on GitHub (pinned to 9e8be09156)

Solutions

  1. Ensure the URI was opened with both read and write persistable permissions.
  2. Avoid SAF URIs from cloud providers for seekable read-write; prefer local file:// paths or local-storage SAF trees.
  3. Verify the document still exists (DocumentFile.fromSingleUri(...).exists()) before opening the descriptor.
  4. Catch IOException and fall back to a file://-based FileStream if the content is available on local storage.
Defensive patterns

Strategy: validation

Validate before calling

// Verify the URI can be opened in 'rw' mode before constructing:
ParcelFileDescriptor pfd = contentResolver.openFileDescriptor(fileUri, "rw");
if (pfd == null) {
    throw new IOException("Cannot open " + fileUri + " in read-write seekable mode");
}
pfd.close(); // will be reopened by FileStreamSAF

Try / catch

try {
    FileStreamSAF stream = new FileStreamSAF(contentResolver, fileUri);
} catch (IOException e) {
    if (e.getMessage() != null && e.getMessage().startsWith("Cannot get the ParcelFileDescriptor")) {
        // provider won't give rw fd — fall back to file:// path if available
        stream = new FileStream(localPath);
    } else throw e;
}

Prevention

When it happens

Trigger: new FileStreamSAF(contentResolver, fileUri) where openFileDescriptor(fileUri, "rw") returns null. Occurs when the URI lacks read/write permission, the provider does not support 'rw' mode (some cloud providers), the document was deleted, or the URI is a virtual file requiring a different stream type.

Common situations: Trying to write to a cloud-provider (Drive/Dropbox) document via SAF; the persistable permission on the URI expired or was revoked; the file was deleted between selection and write; a provider that only supports 'r' mode; opening a virtual file (Google Docs) that has no raw byte representation.

Related errors


AI-assisted analysis of TeamNewPipe/NewPipe@9e8be09156 (2026-08-14). Data as JSON: /api/errors/71c1fd8d3af33794. Report an issue: GitHub.