Example usage for android.provider DocumentsContract buildDocumentUriUsingTree

List of usage examples for android.provider DocumentsContract buildDocumentUriUsingTree

Introduction

In this page you can find the example usage for android.provider DocumentsContract buildDocumentUriUsingTree.

Prototype

public static Uri buildDocumentUriUsingTree(Uri treeUri, String documentId) 

Source Link

Document

Build URI representing the target Document#COLUMN_DOCUMENT_ID in a document provider.

Usage

From source file:com.anthonymandra.support.v4.provider.DocumentsContractApi21.java

public static Uri prepareTreeUri(Uri treeUri) {
    return DocumentsContract.buildDocumentUriUsingTree(treeUri, DocumentsContract.getTreeDocumentId(treeUri));
}

From source file:com.anthonymandra.support.v4.provider.DocumentsContractApi21.java

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;//from   ww w  .  j av  a 2 s . c o  m
    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()]);
}

From source file:com.example.android.directoryselection.DirectorySelectionFragment.java

/**
 * 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.
 *//*w w w  .  java2 s .c om*/
//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);
    }
}

From source file:com.example.android.directoryselection.DirectorySelectionFragment.java

/**
 * 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.
 *//*  w  w w.j a  va2s. c o  m*/
//VisibileForTesting
void createDirectory(Uri uri, String directoryName) {
    ContentResolver contentResolver = getActivity().getContentResolver();
    Uri docUri = DocumentsContract.buildDocumentUriUsingTree(uri, DocumentsContract.getTreeDocumentId(uri));
    Uri directoryUri = DocumentsContract.createDocument(contentResolver, docUri, Document.MIME_TYPE_DIR,
            directoryName);
    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();
    }

}

From source file:com.example.android.scopeddirectoryaccess.ScopedDirectoryAccessFragment.java

private void updateDirectoryEntries(Uri uri) {
    mDirectoryEntries.clear();/*  w w  w  .j  a v  a 2  s  .com*/
    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();
    }
}