Example usage for android.provider DocumentsContract createDocument

List of usage examples for android.provider DocumentsContract createDocument

Introduction

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

Prototype

public static Uri createDocument(ContentProviderClient client, Uri parentDocumentUri, String mimeType,
        String displayName) throws RemoteException 

Source Link

Usage

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

public static Uri createFile(Context context, Uri self, String mimeType, String displayName) {
    return DocumentsContract.createDocument(context.getContentResolver(), self, mimeType, displayName);
}

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  v a2 s  . 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();
    }

}