Java Code Examples for android.provider.DocumentsContract#buildDocumentUriUsingTree()

The following examples show how to use android.provider.DocumentsContract#buildDocumentUriUsingTree() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DocumentsContractApi21.java    From UniFile with Apache License 2.0 7 votes vote down vote up
public static Uri prepareTreeUri(Uri treeUri) {
    String documentId;
    try {
        documentId = DocumentsContract.getDocumentId(treeUri);
        if (documentId == null) {
            throw new IllegalArgumentException();
        }
    } catch (Exception e) {
        // IllegalArgumentException will be raised
        // if DocumentsContract.getDocumentId() failed.
        // But it isn't mentioned the document,
        // catch all kinds of Exception for safety.
        documentId = DocumentsContract.getTreeDocumentId(treeUri);
    }
    return DocumentsContract.buildDocumentUriUsingTree(treeUri, documentId);
}
 
Example 2
Source File: DocumentsContractApi21.java    From adt-leanback-support with Apache License 2.0 6 votes vote down vote up
public static Uri[] listFiles(Context context, Uri self) {
    final ContentResolver resolver = context.getContentResolver();
    final Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(self,
            DocumentsContract.getDocumentId(self));
    final ArrayList<Uri> results = new ArrayList<Uri>();

    Cursor c = null;
    try {
        c = resolver.query(childrenUri, new String[] {
                DocumentsContract.Document.COLUMN_DOCUMENT_ID }, null, null, null);
        while (c.moveToNext()) {
            final String documentId = c.getString(0);
            final Uri documentUri = DocumentsContract.buildDocumentUriUsingTree(self,
                    documentId);
            results.add(documentUri);
        }
    } catch (Exception e) {
        Log.w(TAG, "Failed query: " + e);
    } finally {
        closeQuietly(c);
    }

    return results.toArray(new Uri[results.size()]);
}
 
Example 3
Source File: IOUtils.java    From Dashchan with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static Uri findChildDocument(ContentResolver contentResolver, Uri uri, String displayName) {
	String[] projection = {DocumentsContract.Document.COLUMN_DOCUMENT_ID,
			DocumentsContract.Document.COLUMN_DISPLAY_NAME};
	Uri childUri = DocumentsContract.buildChildDocumentsUriUsingTree(uri,
			DocumentsContract.getDocumentId(uri));
	Cursor cursor = contentResolver.query(childUri, projection, null, null, null);
	try {
		while (cursor.moveToNext()) {
			if (displayName.equals(cursor.getString(1))) {
				return DocumentsContract.buildDocumentUriUsingTree(uri, cursor.getString(0));
			}
		}
	} finally {
		cursor.close();
	}
	return null;
}
 
Example 4
Source File: DocumentsContractApi21.java    From FireFiles with Apache License 2.0 6 votes vote down vote up
public static Uri[] listFiles(Context context, Uri self) {
    final ContentResolver resolver = context.getContentResolver();
    final Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(self,
            DocumentsContract.getDocumentId(self));
    final ArrayList<Uri> results = new ArrayList<Uri>();

    Cursor c = null;
    try {
        c = resolver.query(childrenUri, new String[] {
                DocumentsContract.Document.COLUMN_DOCUMENT_ID }, null, null, null);
        while (c.moveToNext()) {
            final String documentId = c.getString(0);
            final Uri documentUri = DocumentsContract.buildDocumentUriUsingTree(self,
                    documentId);
            results.add(documentUri);
        }
    } catch (Exception e) {
        Log.w(TAG, "Failed query: " + e);
    } finally {
        closeQuietly(c);
    }

    return results.toArray(new Uri[results.size()]);
}
 
Example 5
Source File: DocumentsContractApi21.java    From FireFiles with Apache License 2.0 6 votes vote down vote up
public static Uri[] listFiles(Context context, Uri self) {
    final ContentResolver resolver = context.getContentResolver();
    final Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(self,
            DocumentsContract.getDocumentId(self));
    final ArrayList<Uri> results = new ArrayList<Uri>();

    Cursor c = null;
    try {
        c = resolver.query(childrenUri, new String[] {
                DocumentsContract.Document.COLUMN_DOCUMENT_ID }, null, null, null);
        while (c.moveToNext()) {
            final String documentId = c.getString(0);
            final Uri documentUri = DocumentsContract.buildDocumentUriUsingTree(self,
                    documentId);
            results.add(documentUri);
        }
    } catch (Exception e) {
        Log.w(TAG, "Failed query: " + e);
    } finally {
        closeQuietly(c);
    }

    return results.toArray(new Uri[results.size()]);
}
 
Example 6
Source File: DocumentsContractApi21.java    From FireFiles with Apache License 2.0 6 votes vote down vote up
public static Uri[] listFiles(Context context, Uri self) {
    final ContentResolver resolver = context.getContentResolver();
    final Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(self,
            DocumentsContract.getDocumentId(self));
    final ArrayList<Uri> results = new ArrayList<Uri>();

    Cursor c = null;
    try {
        c = resolver.query(childrenUri, new String[] {
                DocumentsContract.Document.COLUMN_DOCUMENT_ID }, null, null, null);
        while (c.moveToNext()) {
            final String documentId = c.getString(0);
            final Uri documentUri = DocumentsContract.buildDocumentUriUsingTree(self,
                    documentId);
            results.add(documentUri);
        }
    } catch (Exception e) {
        Log.w(TAG, "Failed query: " + e);
    } finally {
        closeQuietly(c);
    }

    return results.toArray(new Uri[results.size()]);
}
 
Example 7
Source File: AdapterDocuments.java    From microMathematics with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void setUri(Uri uri_)
{
    if (this.uri == null && isTreeUri(uri_))
    {
        try
        {
            ctx.getContentResolver().takePersistableUriPermission(uri_,
                    Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        this.uri = DocumentsContract.buildDocumentUriUsingTree(uri_, DocumentsContract.getTreeDocumentId(uri_));
    }
    else
    {
        this.uri = uri_;
    }
}
 
Example 8
Source File: DirectorySelectionFragment.java    From android-DirectorySelection with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a directory under the directory represented as the uri in the argument.
 *
 * @param uri The uri of the directory under which a new directory is created.
 * @param directoryName The directory name of a new directory.
 */
//VisibileForTesting
void createDirectory(Uri uri, String directoryName) {
    ContentResolver contentResolver = getActivity().getContentResolver();
    Uri docUri = DocumentsContract.buildDocumentUriUsingTree(uri,
            DocumentsContract.getTreeDocumentId(uri));
    Uri directoryUri = null;
    try {
        directoryUri = DocumentsContract
                .createDocument(contentResolver, docUri, Document.MIME_TYPE_DIR, directoryName);
    } catch (IOException e) {
        Log.w(TAG, "IOException", e);
    }
    if (directoryUri != null) {
        Log.i(TAG, String.format(
                "Created directory : %s, Document Uri : %s, Created directory Uri : %s",
                directoryName, docUri, directoryUri));
        Toast.makeText(getActivity(), String.format("Created a directory [%s]",
                directoryName), Toast.LENGTH_SHORT).show();
    } else {
        Log.w(TAG, String.format("Failed to create a directory : %s, Uri %s", directoryName,
                docUri));
        Toast.makeText(getActivity(), String.format("Failed to created a directory [%s] : ",
                directoryName), Toast.LENGTH_SHORT).show();
    }

}
 
Example 9
Source File: SafUtils.java    From SAI with GNU General Public License v3.0 5 votes vote down vote up
public static Uri buildChildDocumentUri(Uri directoryUri, String childDisplayName) {
    if (!isTreeUri(directoryUri))
        throw new IllegalArgumentException("directoryUri must be a tree uri");

    String rootPath = getRootForPath(directoryUri);
    String directoryPath = getPathWithoutRoot(directoryUri);

    String childPath = rootPath + ":" + directoryPath + "/" + FileUtils.buildValidFatFilename(childDisplayName);

    return DocumentsContract.buildDocumentUriUsingTree(directoryUri, childPath);
}
 
Example 10
Source File: FileUtils.java    From syncthing-android with Mozilla Public License 2.0 5 votes vote down vote up
@Nullable
@TargetApi(21)
public static String getAbsolutePathFromSAFUri(Context context, @Nullable final Uri safResultUri) {
    Uri treeUri = DocumentsContract.buildDocumentUriUsingTree(safResultUri,
        DocumentsContract.getTreeDocumentId(safResultUri));
    return getAbsolutePathFromTreeUri(context, treeUri);
}
 
Example 11
Source File: ScopedDirectoryAccessFragment.java    From android-ScopedDirectoryAccess with Apache License 2.0 5 votes vote down vote up
private void updateDirectoryEntries(Uri uri) {
    mDirectoryEntries.clear();
    ContentResolver contentResolver = getActivity().getContentResolver();
    Uri docUri = DocumentsContract.buildDocumentUriUsingTree(uri,
            DocumentsContract.getTreeDocumentId(uri));
    Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(uri,
            DocumentsContract.getTreeDocumentId(uri));

    try (Cursor docCursor = contentResolver
            .query(docUri, DIRECTORY_SELECTION, null, null, null)) {
        while (docCursor != null && docCursor.moveToNext()) {
            mCurrentDirectoryTextView.setText(docCursor.getString(docCursor.getColumnIndex(
                    DocumentsContract.Document.COLUMN_DISPLAY_NAME)));
        }
    }

    try (Cursor childCursor = contentResolver
            .query(childrenUri, DIRECTORY_SELECTION, null, null, null)) {
        while (childCursor != null && childCursor.moveToNext()) {
            DirectoryEntry entry = new DirectoryEntry();
            entry.fileName = childCursor.getString(childCursor.getColumnIndex(
                    DocumentsContract.Document.COLUMN_DISPLAY_NAME));
            entry.mimeType = childCursor.getString(childCursor.getColumnIndex(
                    DocumentsContract.Document.COLUMN_MIME_TYPE));
            mDirectoryEntries.add(entry);
        }

        if (mDirectoryEntries.isEmpty()) {
            mNothingInDirectoryTextView.setVisibility(View.VISIBLE);
        } else {
            mNothingInDirectoryTextView.setVisibility(View.GONE);
        }
        mAdapter.setDirectoryEntries(mDirectoryEntries);
        mAdapter.notifyDataSetChanged();
    }
}
 
Example 12
Source File: DocumentTreeFS.java    From edslite with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean nextResult(Cursor c)
{
    if (!c.isNull(1) && _childName.equals(c.getString(1)))
    {
        final String documentId = c.getString(0);
        _uri = DocumentsContract.buildDocumentUriUsingTree(_documentUri,
                documentId);
        return false;
    }
    return true;
}
 
Example 13
Source File: DocumentTreeFS.java    From edslite with GNU General Public License v2.0 5 votes vote down vote up
public DocumentTreeFS(Context context, Uri rootUri)
{
    _context = context;
    _rootPath = new DocumentPath(
            DocumentsContract.buildDocumentUriUsingTree(
                    rootUri,
                    DocumentsContract.getTreeDocumentId(rootUri)
            )
    );
}
 
Example 14
Source File: AdapterDocuments.java    From microMathematics with GNU General Public License v3.0 4 votes vote down vote up
public final static ArrayList<SAFItem> getChildren(Context ctx, Uri u)
{
    Cursor c = null;
    try
    {
        ContentResolver cr = ctx.getContentResolver();
        String document_id = DocumentsContract.getDocumentId(u);
        Uri children_uri = DocumentsContract.buildChildDocumentsUriUsingTree(u, document_id);
        c = cr.query(children_uri, projection, null, null, null);
        if (c != null)
        {
            ArrayList<SAFItem> tmp_list = new ArrayList<>();
            if (c.getCount() == 0)
            {
                return tmp_list;
            }
            int ici = c.getColumnIndex(Document.COLUMN_DOCUMENT_ID);
            int nci = c.getColumnIndex(Document.COLUMN_DISPLAY_NAME);
            int sci = c.getColumnIndex(Document.COLUMN_SIZE);
            int mci = c.getColumnIndex(Document.COLUMN_MIME_TYPE);
            int dci = c.getColumnIndex(Document.COLUMN_LAST_MODIFIED);
            c.moveToFirst();
            do
            {
                SAFItem item = new SAFItem();
                String id = c.getString(ici);
                item.origin = DocumentsContract.buildDocumentUriUsingTree(u, id);
                item.attr = c.getString(mci);
                item.dir = Document.MIME_TYPE_DIR.equals(item.attr);
                item.name = c.getString(nci);
                item.size = c.getLong(sci);
                item.date = new Date(c.getLong(dci));
                if (item.dir)
                {
                    item.size = -1;
                    item.attr = Integer.toString(getDirItemsNumber(ctx, (Uri) item.origin)) + " "
                            + ctx.getString(R.string.dialog_list_items);
                }
                tmp_list.add(item);
            }
            while (c.moveToNext());
            return tmp_list;
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        if (c != null)
        {
            c.close();
        }
    }
    return null;
}
 
Example 15
Source File: DocumentsContractApi21.java    From FireFiles with Apache License 2.0 4 votes vote down vote up
public static Uri prepareTreeUri(Uri treeUri) {
    return DocumentsContract.buildDocumentUriUsingTree(treeUri,
            DocumentsContract.getTreeDocumentId(treeUri));
}
 
Example 16
Source File: DocumentsContractApi21.java    From FireFiles with Apache License 2.0 4 votes vote down vote up
public static Uri prepareTreeUri(Uri treeUri) {
    return DocumentsContract.buildDocumentUriUsingTree(treeUri,
            DocumentsContract.getTreeDocumentId(treeUri));
}
 
Example 17
Source File: DocumentsContractApi21.java    From FireFiles with Apache License 2.0 4 votes vote down vote up
public static Uri prepareTreeUri(Uri treeUri) {
    return DocumentsContract.buildDocumentUriUsingTree(treeUri,
            DocumentsContract.getTreeDocumentId(treeUri));
}
 
Example 18
Source File: DocumentsContractApi21.java    From adt-leanback-support with Apache License 2.0 4 votes vote down vote up
public static Uri prepareTreeUri(Uri treeUri) {
    return DocumentsContract.buildDocumentUriUsingTree(treeUri,
            DocumentsContract.getTreeDocumentId(treeUri));
}
 
Example 19
Source File: DocumentTreeFS.java    From edslite with GNU General Public License v2.0 4 votes vote down vote up
@Override
public com.sovworks.eds.fs.Directory.Contents list() throws IOException
{
    final Uri uri = _path.getDocumentUri();
    final Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(uri,
            DocumentsContract.getDocumentId(uri));
    final ContentResolver resolver = _context.getContentResolver();
    final Cursor cursor = resolver.query(
            childrenUri,
            new String[]{DocumentsContract.Document.COLUMN_DOCUMENT_ID},
            DocumentsContract.Document.COLUMN_DOCUMENT_ID + "!=?",
            new String[] { ".android_secure" },
            null
    );
    return new com.sovworks.eds.fs.Directory.Contents()
    {
        @Override
        public void close() throws IOException
        {
            if (cursor != null)
                cursor.close();
        }

        @Override
        public Iterator<Path> iterator()
        {
            return new Iterator<Path>()
            {
                @Override
                public void remove()
                {

                }

                @Override
                public Path next()
                {
                    if (cursor == null)
                        throw new NoSuchElementException();
                    final String documentId = cursor.getString(0);
                    final Uri documentUri = DocumentsContract.buildDocumentUriUsingTree(uri,
                            documentId);
                    _hasNext = cursor.moveToNext();
                    Path newPath = new DocumentPath(documentUri);
                    synchronized (_parentsCache)
                    {
                        _parentsCache.put(newPath, _path);
                    }
                    return newPath;
                }

                @Override
                public boolean hasNext()
                {
                    return _hasNext;
                }

                private boolean _hasNext = cursor != null && cursor.moveToFirst();
            };
        }
    };

}
 
Example 20
Source File: DirectorySelectionFragment.java    From android-DirectorySelection with Apache License 2.0 4 votes vote down vote up
/**
 * Updates the current directory of the uri passed as an argument and its children directories.
 * And updates the {@link #mRecyclerView} depending on the contents of the children.
 *
 * @param uri The uri of the current directory.
 */
//VisibileForTesting
void updateDirectoryEntries(Uri uri) {
    ContentResolver contentResolver = getActivity().getContentResolver();
    Uri docUri = DocumentsContract.buildDocumentUriUsingTree(uri,
            DocumentsContract.getTreeDocumentId(uri));
    Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(uri,
            DocumentsContract.getTreeDocumentId(uri));

    Cursor docCursor = contentResolver.query(docUri, new String[]{
            Document.COLUMN_DISPLAY_NAME, Document.COLUMN_MIME_TYPE}, null, null, null);
    try {
        while (docCursor.moveToNext()) {
            Log.d(TAG, "found doc =" + docCursor.getString(0) + ", mime=" + docCursor
                    .getString(1));
            mCurrentDirectoryUri = uri;
            mCurrentDirectoryTextView.setText(docCursor.getString(0));
            mCreateDirectoryButton.setEnabled(true);
        }
    } finally {
        closeQuietly(docCursor);
    }

    Cursor childCursor = contentResolver.query(childrenUri, new String[]{
            Document.COLUMN_DISPLAY_NAME, Document.COLUMN_MIME_TYPE}, null, null, null);
    try {
        List<DirectoryEntry> directoryEntries = new ArrayList<>();
        while (childCursor.moveToNext()) {
            Log.d(TAG, "found child=" + childCursor.getString(0) + ", mime=" + childCursor
                    .getString(1));
            DirectoryEntry entry = new DirectoryEntry();
            entry.fileName = childCursor.getString(0);
            entry.mimeType = childCursor.getString(1);
            directoryEntries.add(entry);
        }
        mAdapter.setDirectoryEntries(directoryEntries);
        mAdapter.notifyDataSetChanged();
    } finally {
        closeQuietly(childCursor);
    }
}