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

The following examples show how to use android.provider.DocumentsContract#buildChildDocumentsUriUsingTree() . 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: DocumentTreeFS.java    From edslite with GNU General Public License v2.0 6 votes vote down vote up
private void listChildren(ResultReceiver res, Uri uri, String... columns)
{
    final ContentResolver resolver = _context.getContentResolver();
    final Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(uri,
            DocumentsContract.getDocumentId(uri));
    Cursor c = null;
    try
    {
        c = resolver.query(childrenUri, columns, null, null, null);
        if (c != null)
            while (c.moveToNext())
            {
                if (!res.nextResult(c))
                    break;
            }
    }
    catch (Exception e)
    {
        if(GlobalConfig.isDebug())
            Logger.log(e);
    }
    finally
    {
        closeQuietly(c);
    }
}
 
Example 2
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 3
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 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 UniFile 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<>();

    Cursor c = null;
    try {
        c = resolver.query(childrenUri, new String[] {
                DocumentsContract.Document.COLUMN_DOCUMENT_ID }, null, null, null);
        if (null != c) {
            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: 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 7
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 8
Source File: AdapterDocuments.java    From microMathematics with GNU General Public License v3.0 5 votes vote down vote up
private static int getDirItemsNumber(Context ctx, Uri u)
{
    int itemsNumber = 0;
    Cursor c = null;
    try
    {
        ContentResolver cr = ctx.getContentResolver();
        String dirId = DocumentsContract.getDocumentId(u);
        Uri dirUri = DocumentsContract.buildChildDocumentsUriUsingTree(u, dirId);
        c = cr.query(dirUri, projection, null, null, null);
        if (c != null)
        {
            itemsNumber = c.getCount();
        }
    }
    catch (Exception e)
    {
        // notning to do;
    }
    finally
    {
        if (c != null)
        {
            c.close();
        }
    }
    return itemsNumber;
}
 
Example 9
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 10
Source File: FileUtil.java    From Hentoid with Apache License 2.0 5 votes vote down vote up
static List<DocumentFile> listDocumentFiles(
        @NonNull final Context context,
        @NonNull final DocumentFile parent,
        @NonNull final ContentProviderClient client,
        final FileHelper.NameFilter nameFilter,
        boolean listFolders,
        boolean listFiles) {
    final List<ImmutableTriple<Uri, String, Long>> results = new ArrayList<>();

    final Uri searchUri = DocumentsContract.buildChildDocumentsUriUsingTree(parent.getUri(), DocumentsContract.getDocumentId(parent.getUri()));
    try (Cursor c = client.query(searchUri, new String[]{
            DocumentsContract.Document.COLUMN_DOCUMENT_ID,
            DocumentsContract.Document.COLUMN_DISPLAY_NAME,
            DocumentsContract.Document.COLUMN_MIME_TYPE,
            DocumentsContract.Document.COLUMN_SIZE}, null, null, null)) {
        if (c != null)
            while (c.moveToNext()) {
                final String documentId = c.getString(0);
                final String documentName = c.getString(1);
                boolean isFolder = c.getString(2).equals(DocumentsContract.Document.MIME_TYPE_DIR);
                final Long documentSize = c.getLong(3);

                // FileProvider doesn't take query selection arguments into account, so the selection has to be done manually
                if ((null == nameFilter || nameFilter.accept(documentName)) && ((listFiles && !isFolder) || (listFolders && isFolder)))
                    results.add(new ImmutableTriple<>(DocumentsContract.buildDocumentUriUsingTree(parent.getUri(), documentId), documentName, documentSize));
            }
    } catch (Exception e) {
        Timber.w(e, "Failed query");
    }

    return convertFromUris(context, results);
}
 
Example 11
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 12
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 13
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);
    }
}