CWAC-Document openInputStream fromTreeUri

I’m currently using CWAC-Document I needed this hack in TreeDocumentFile;

public InputStream openInputStream() throws FileNotFoundException {
//        throw new UnsupportedOperationException("Cannot open a stream on a tree");
// humpty's hack: force to get an inputstream for a treeUri
// usually works if provider is a local external secondary sdcard
 return Context.getContentResolver().openInputStream(getUri());

So my question is: What is the proper way to do this without the hack ?

I do not understand what that code does. I would expect an IOException, just as you would get if you tried opening a FileInputStream on a directory. What are you getting for that InputStream?

Some excerpts;

// I used ACTION_OPEN_DOCUMENT_TREE to get a grant and a saf_treeUri (which is a directory),
// then;

DocumentFileCompat saf_dir, saf_file;

saf_dir = DocumentFileCompat.fromTreeUri(Run.this, saf_treeUri);

// later on, I look for a file inside the directory..

saf_file = saf_dir.findFile(fileName);

// assuming one is found and tested is a file (not a directory), 
// I try to open the file for input;

BufferedReader buf = null;
  buf = new BufferedReader(new InputStreamReader(saf_file.openInputStream()));
catch (Exception e)
  buf = null;
if (buf == null) return null;			// failed

The problem was that saf_file was a TreeDocumentFile (as opposed to e.g SingleDocumentFile)
an gave an error. Which led me to make the hack (which is working).

I think I tried casting a TreeDocumentFile to SingleDocumentFile, but that wouldn’t compile.
So I must be missing something, or there is some other way to open a file InputStream ?

That is a bug in CWAC-Document. I will work on fixing it soon. Thanks for pointing this out!

OK, I just pushed out a v0.4.0 that fixes this bug. The repo also has a demo app.

If you run into any problems with it, let me know!

v0.4.0 is working, but there was an unrelated side effect.
Where before, I was able to later on renameTo(String displayName)
with a TreeDocumentFile, I cannot with SingleDocumentFile ;

How to rename a SingleDocumentFile ?

Ummm… wait until sometime tomorrow, then upgrade to v0.4.1. :sweat_smile:

In other words, I will try to carve out time to fix it tomorrow, and I’ll post a reply here when I do.

Thanks for pointing this out!

There’s no rush. Thanks.

OK, v0.4.1 is out. I cloned the renameTo() logic from the tree-Uri scenario into the single-document-Uri scenario. Note that it will still throw an exception on devices running older than API Level 21 (Android 5.0) — that’s out of my control, as the framework API that I depend upon did not exist until then.

Let me know if you encounter further problems, and thanks for your patience!