Example usage for android.provider DocumentsContract getTreeDocumentId

List of usage examples for android.provider DocumentsContract getTreeDocumentId

Introduction

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

Prototype

public static String getTreeDocumentId(Uri documentUri) 

Source Link

Document

Extract the via Document#COLUMN_DOCUMENT_ID from the given URI.

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.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.
 *///  www  .  java 2s.  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.
 *///from   w  w  w  . j a  v a 2s .c om
//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();//  www  .j  av  a2  s  .co m
    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();
    }
}