类android.provider.DocumentsContract.Document源码实例Demo

下面列出了怎么用android.provider.DocumentsContract.Document的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: android_9.0.0_r45   文件: DocumentsProvider.java
/**
 * Return a list of streamable MIME types matching the filter, which can be passed to
 * {@link #openTypedDocument(String, String, Bundle, CancellationSignal)}.
 *
 * <p>The default implementation returns a MIME type provided by
 * {@link #queryDocument(String, String[])} as long as it matches the filter and the document
 * does not have the {@link Document#FLAG_VIRTUAL_DOCUMENT} flag set.
 *
 * <p>Virtual documents must have at least one streamable format.
 *
 * @see #getStreamTypes(Uri, String)
 * @see #openTypedDocument(String, String, Bundle, CancellationSignal)
 */
public String[] getDocumentStreamTypes(String documentId, String mimeTypeFilter) {
    Cursor cursor = null;
    try {
        cursor = queryDocument(documentId, null);
        if (cursor.moveToFirst()) {
            final String mimeType =
                cursor.getString(cursor.getColumnIndexOrThrow(Document.COLUMN_MIME_TYPE));
            final long flags =
                cursor.getLong(cursor.getColumnIndexOrThrow(Document.COLUMN_FLAGS));
            if ((flags & Document.FLAG_VIRTUAL_DOCUMENT) == 0 && mimeType != null &&
                    mimeTypeMatches(mimeTypeFilter, mimeType)) {
                return new String[] { mimeType };
            }
        }
    } catch (FileNotFoundException e) {
        return null;
    } finally {
        IoUtils.closeQuietly(cursor);
    }

    // No streamable MIME types.
    return null;
}
 
源代码2 项目: JPPF   文件: AppStorageProvider.java
/**
 * Add a row in the specified cursor for the specified file.
 * @param child the file for which to add a row.
 * @param cursor the cursor to add a row to.
 */
private void createRowForChild(File child, MatrixCursor cursor) {
  MatrixCursor.RowBuilder row = cursor.newRow();
  String type = getContext().getContentResolver().getType(Uri.fromFile(child));
  Log.v(LOG_TAG, "createRowForChild(child=" + child + ") type=" + type);
  if (type == null) type = child.isDirectory() ? Document.MIME_TYPE_DIR : "application/octet-stream";
  int flags = child.isDirectory()
    ? /*Document.FLAG_DIR_PREFERS_GRID |*/ Document.FLAG_DIR_PREFERS_LAST_MODIFIED | Document.FLAG_DIR_SUPPORTS_CREATE
    : /*Document.FLAG_SUPPORTS_WRITE*/ 0;
  row.add(Document.COLUMN_FLAGS, flags);
  row.add(Document.COLUMN_DISPLAY_NAME, child.getName());
  row.add(Document.COLUMN_DOCUMENT_ID, getDocumentId(child));
  row.add(Document.COLUMN_MIME_TYPE, type);
  row.add(Document.COLUMN_SIZE, child.isDirectory() ? child.getTotalSpace() - child.getFreeSpace() : child.length());
  row.add(Document.COLUMN_LAST_MODIFIED, null);
}
 
源代码3 项目: microMathematics   文件: AdapterDocuments.java
@Override
public void createFolder(String new_name)
{
    try
    {
        Uri new_uri = DocumentsContract.createDocument(ctx.getContentResolver(), uri, Document.MIME_TYPE_DIR,
                new_name);
        if (new_uri != null)
        {
            notifyRefr(new_name);
            return;
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    notify(ctx.getString(R.string.fman_create_folder_error, new_name), CommanderIf.OPERATION_FAILED);
}
 
private void recursiveDeleteFolder(DocumentMetadata metadata) throws IOException {
  // Fetch the latest children just in case our cache is out of date.
  metadata.loadChildren(mClient);
  for (DocumentMetadata child : metadata.getChildren().values()) {
    if (Document.MIME_TYPE_DIR.equals(child.getMimeType())) {
      recursiveDeleteFolder(child);
    } else {
      deleteFile(child);
    }
  }

  final Uri uri = metadata.getUri();
  mClient.rmdir(uri.toString());
  mCache.remove(uri);

  revokeDocumentPermission(toDocumentId(uri));
}
 
源代码5 项目: droidddle   文件: LocalStorageProvider.java
private void includeFile(final MatrixCursor result, final File file) throws FileNotFoundException {
    final MatrixCursor.RowBuilder row = result.newRow();
    // These columns are required
    row.add(Document.COLUMN_DOCUMENT_ID, file.getAbsolutePath());
    row.add(Document.COLUMN_DISPLAY_NAME, file.getName());
    String mimeType = getDocumentType(file.getAbsolutePath());
    row.add(Document.COLUMN_MIME_TYPE, mimeType);
    int flags = file.canWrite() ? Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE : 0;
    // We only show thumbnails for image files - expect a call to
    // openDocumentThumbnail for each file that has
    // this flag set
    if (mimeType.startsWith("image/"))
        flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
    row.add(Document.COLUMN_FLAGS, flags);
    // COLUMN_SIZE is required, but can be null
    row.add(Document.COLUMN_SIZE, file.length());
    // These columns are optional
    row.add(Document.COLUMN_LAST_MODIFIED, file.lastModified());
    // Document.COLUMN_ICON can be a resource id identifying a custom icon.
    // The system provides default icons
    // based on mime type
    // Document.COLUMN_SUMMARY is optional additional information about the
    // file
}
 
源代码6 项目: droidddle   文件: LocalStorageProvider.java
@Override
public String getDocumentType(final String documentId) throws FileNotFoundException {
    File file = new File(documentId);
    if (file.isDirectory())
        return Document.MIME_TYPE_DIR;
    // From FileProvider.getType(Uri)
    final int lastDot = file.getName().lastIndexOf('.');
    if (lastDot >= 0) {
        final String extension = file.getName().substring(lastDot + 1);
        final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (mime != null) {
            return mime;
        }
    }
    return "application/octet-stream";
}
 
private void includeFile(final MatrixCursor result, final File file)
        throws FileNotFoundException {
    final MatrixCursor.RowBuilder row = result.newRow();
    // These columns are required
    row.add(Document.COLUMN_DOCUMENT_ID, file.getAbsolutePath());
    row.add(Document.COLUMN_DISPLAY_NAME, file.getName());
    String mimeType = getDocumentType(file.getAbsolutePath());
    row.add(Document.COLUMN_MIME_TYPE, mimeType);
    int flags = file.canWrite() ? Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE
            : 0;
    // We only show thumbnails for image files - expect a call to
    // openDocumentThumbnail for each file that has
    // this flag set
    if (mimeType.startsWith("image/"))
        flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
    row.add(Document.COLUMN_FLAGS, flags);
    // COLUMN_SIZE is required, but can be null
    row.add(Document.COLUMN_SIZE, file.length());
    // These columns are optional
    row.add(Document.COLUMN_LAST_MODIFIED, file.lastModified());
    // Document.COLUMN_ICON can be a resource id identifying a custom icon.
    // The system provides default icons
    // based on mime type
    // Document.COLUMN_SUMMARY is optional additional information about the
    // file
}
 
@Override
public String getDocumentType(final String documentId) throws FileNotFoundException {
    File file = new File(documentId);
    if (file.isDirectory())
        return Document.MIME_TYPE_DIR;
    // From FileProvider.getType(Uri)
    final int lastDot = file.getName().lastIndexOf('.');
    if (lastDot >= 0) {
        final String extension = file.getName().substring(lastDot + 1);
        final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (mime != null) {
            return mime;
        }
    }
    return "application/octet-stream";
}
 
源代码9 项目: qiniu-lab-android   文件: LocalStorageProvider.java
private void includeFile(final MatrixCursor result, final File file)
        throws FileNotFoundException {
    final MatrixCursor.RowBuilder row = result.newRow();
    // These columns are required
    row.add(Document.COLUMN_DOCUMENT_ID, file.getAbsolutePath());
    row.add(Document.COLUMN_DISPLAY_NAME, file.getName());
    String mimeType = getDocumentType(file.getAbsolutePath());
    row.add(Document.COLUMN_MIME_TYPE, mimeType);
    int flags = file.canWrite() ? Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE
            : 0;
    // We only show thumbnails for image files - expect a call to
    // openDocumentThumbnail for each file that has
    // this flag set
    if (mimeType.startsWith("image/"))
        flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
    row.add(Document.COLUMN_FLAGS, flags);
    // COLUMN_SIZE is required, but can be null
    row.add(Document.COLUMN_SIZE, file.length());
    // These columns are optional
    row.add(Document.COLUMN_LAST_MODIFIED, file.lastModified());
    // Document.COLUMN_ICON can be a resource id identifying a custom icon.
    // The system provides default icons
    // based on mime type
    // Document.COLUMN_SUMMARY is optional additional information about the
    // file
}
 
源代码10 项目: qiniu-lab-android   文件: LocalStorageProvider.java
@Override
public String getDocumentType(final String documentId) throws FileNotFoundException {
    File file = new File(documentId);
    if (file.isDirectory())
        return Document.MIME_TYPE_DIR;
    // From FileProvider.getType(Uri)
    final int lastDot = file.getName().lastIndexOf('.');
    if (lastDot >= 0) {
        final String extension = file.getName().substring(lastDot + 1);
        final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (mime != null) {
            return mime;
        }
    }
    return "application/octet-stream";
}
 
源代码11 项目: droid-stealth   文件: LocalStorageProvider.java
private void includeFile(final MatrixCursor result, final File file)
        throws FileNotFoundException {
    final MatrixCursor.RowBuilder row = result.newRow();
    // These columns are required
    row.add(Document.COLUMN_DOCUMENT_ID, file.getAbsolutePath());
    row.add(Document.COLUMN_DISPLAY_NAME, file.getName());
    String mimeType = getDocumentType(file.getAbsolutePath());
    row.add(Document.COLUMN_MIME_TYPE, mimeType);
    int flags = file.canWrite() ? Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE
            : 0;
    // We only show thumbnails for image files - expect a call to
    // openDocumentThumbnail for each file that has
    // this flag set
    if (mimeType.startsWith("image/"))
        flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
    row.add(Document.COLUMN_FLAGS, flags);
    // COLUMN_SIZE is required, but can be null
    row.add(Document.COLUMN_SIZE, file.length());
    // These columns are optional
    row.add(Document.COLUMN_LAST_MODIFIED, file.lastModified());
    // Document.COLUMN_ICON can be a resource id identifying a custom icon.
    // The system provides default icons
    // based on mime type
    // Document.COLUMN_SUMMARY is optional additional information about the
    // file
}
 
源代码12 项目: droid-stealth   文件: LocalStorageProvider.java
@Override
public String getDocumentType(final String documentId) throws FileNotFoundException {
    File file = new File(documentId);
    if (file.isDirectory())
        return Document.MIME_TYPE_DIR;
    // From FileProvider.getType(Uri)
    final int lastDot = file.getName().lastIndexOf('.');
    if (lastDot >= 0) {
        final String extension = file.getName().substring(lastDot + 1);
        final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (mime != null) {
            return mime;
        }
    }
    return "application/octet-stream";
}
 
源代码13 项目: Effects-Pro   文件: LocalStorageProvider.java
private void includeFile(final MatrixCursor result, final File file)
		throws FileNotFoundException {
	final MatrixCursor.RowBuilder row = result.newRow();
	// These columns are required
	row.add(Document.COLUMN_DOCUMENT_ID, file.getAbsolutePath());
	row.add(Document.COLUMN_DISPLAY_NAME, file.getName());
	String mimeType = getDocumentType(file.getAbsolutePath());
	row.add(Document.COLUMN_MIME_TYPE, mimeType);
	int flags = file.canWrite() ? Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE
			: 0;
	// We only show thumbnails for image files - expect a call to
	// openDocumentThumbnail for each file that has
	// this flag set
	if (mimeType.startsWith("image/"))
		flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
	row.add(Document.COLUMN_FLAGS, flags);
	// COLUMN_SIZE is required, but can be null
	row.add(Document.COLUMN_SIZE, file.length());
	// These columns are optional
	row.add(Document.COLUMN_LAST_MODIFIED, file.lastModified());
	// Document.COLUMN_ICON can be a resource id identifying a custom icon.
	// The system provides default icons
	// based on mime type
	// Document.COLUMN_SUMMARY is optional additional information about the
	// file
}
 
源代码14 项目: Effects-Pro   文件: LocalStorageProvider.java
@Override
public String getDocumentType(final String documentId) throws FileNotFoundException {
	File file = new File(documentId);
	if (file.isDirectory())
		return Document.MIME_TYPE_DIR;
	// From FileProvider.getType(Uri)
	final int lastDot = file.getName().lastIndexOf('.');
	if (lastDot >= 0) {
		final String extension = file.getName().substring(lastDot + 1);
		final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
		if (mime != null) {
			return mime;
		}
	}
	return "application/octet-stream";
}
 
源代码15 项目: filechooser   文件: LocalStorageProvider.java
private void includeFile(final MatrixCursor result, final File file)
        throws FileNotFoundException {
    final MatrixCursor.RowBuilder row = result.newRow();
    // These columns are required
    row.add(Document.COLUMN_DOCUMENT_ID, file.getAbsolutePath());
    row.add(Document.COLUMN_DISPLAY_NAME, file.getName());
    String mimeType = getDocumentType(file.getAbsolutePath());
    row.add(Document.COLUMN_MIME_TYPE, mimeType);
    int flags = file.canWrite() ? Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE
            : 0;
    // We only show thumbnails for image files - expect a call to
    // openDocumentThumbnail for each file that has
    // this flag set
    if (mimeType.startsWith("image/"))
        flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
    row.add(Document.COLUMN_FLAGS, flags);
    // COLUMN_SIZE is required, but can be null
    row.add(Document.COLUMN_SIZE, file.length());
    // These columns are optional
    row.add(Document.COLUMN_LAST_MODIFIED, file.lastModified());
    // Document.COLUMN_ICON can be a resource id identifying a custom icon.
    // The system provides default icons
    // based on mime type
    // Document.COLUMN_SUMMARY is optional additional information about the
    // file
}
 
源代码16 项目: filechooser   文件: LocalStorageProvider.java
@Override
public String getDocumentType(final String documentId) throws FileNotFoundException {
    File file = new File(documentId);
    if (file.isDirectory())
        return Document.MIME_TYPE_DIR;
    // From FileProvider.getType(Uri)
    final int lastDot = file.getName().lastIndexOf('.');
    if (lastDot >= 0) {
        final String extension = file.getName().substring(lastDot + 1);
        final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (mime != null) {
            return mime;
        }
    }
    return "application/octet-stream";
}
 
源代码17 项目: secrecy   文件: LocalStorageProvider.java
private void includeFile(final MatrixCursor result, final File file)
        throws FileNotFoundException {
    final MatrixCursor.RowBuilder row = result.newRow();
    // These columns are required
    row.add(Document.COLUMN_DOCUMENT_ID, file.getAbsolutePath());
    row.add(Document.COLUMN_DISPLAY_NAME, file.getName());
    String mimeType = getDocumentType(file.getAbsolutePath());
    row.add(Document.COLUMN_MIME_TYPE, mimeType);
    int flags = file.canWrite() ? Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE
            : 0;
    // We only show thumbnails for image files - expect a call to
    // openDocumentThumbnail for each file that has
    // this flag set
    if (mimeType.startsWith("image/"))
        flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
    row.add(Document.COLUMN_FLAGS, flags);
    // COLUMN_SIZE is required, but can be null
    row.add(Document.COLUMN_SIZE, file.length());
    // These columns are optional
    row.add(Document.COLUMN_LAST_MODIFIED, file.lastModified());
    // Document.COLUMN_ICON can be a resource id identifying a custom icon.
    // The system provides default icons
    // based on mime type
    // Document.COLUMN_SUMMARY is optional additional information about the
    // file
}
 
源代码18 项目: secrecy   文件: LocalStorageProvider.java
@Override
public String getDocumentType(final String documentId) throws FileNotFoundException {
    File file = new File(documentId);
    if (file.isDirectory())
        return Document.MIME_TYPE_DIR;
    // From FileProvider.getType(Uri)
    final int lastDot = file.getName().lastIndexOf('.');
    if (lastDot >= 0) {
        final String extension = file.getName().substring(lastDot + 1);
        final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (mime != null) {
            return mime;
        }
    }
    return "application/octet-stream";
}
 
源代码19 项目: Readily   文件: LocalStorageProvider.java
private void includeFile(final MatrixCursor result, final File file)
        throws FileNotFoundException {
    final MatrixCursor.RowBuilder row = result.newRow();
    // These columns are required
    row.add(Document.COLUMN_DOCUMENT_ID, file.getAbsolutePath());
    row.add(Document.COLUMN_DISPLAY_NAME, file.getName());
    String mimeType = getDocumentType(file.getAbsolutePath());
    row.add(Document.COLUMN_MIME_TYPE, mimeType);
    int flags = file.canWrite() ? Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE
            : 0;
    // We only show thumbnails for image files - expect a call to
    // openDocumentThumbnail for each file that has
    // this flag set
    if (mimeType.startsWith("image/"))
        flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
    row.add(Document.COLUMN_FLAGS, flags);
    // COLUMN_SIZE is required, but can be null
    row.add(Document.COLUMN_SIZE, file.length());
    // These columns are optional
    row.add(Document.COLUMN_LAST_MODIFIED, file.lastModified());
    // Document.COLUMN_ICON can be a resource id identifying a custom icon.
    // The system provides default icons
    // based on mime type
    // Document.COLUMN_SUMMARY is optional additional information about the
    // file
}
 
源代码20 项目: Readily   文件: LocalStorageProvider.java
@Override
public String getDocumentType(final String documentId) throws FileNotFoundException {
    File file = new File(documentId);
    if (file.isDirectory())
        return Document.MIME_TYPE_DIR;
    // From FileProvider.getType(Uri)
    final int lastDot = file.getName().lastIndexOf('.');
    if (lastDot >= 0) {
        final String extension = file.getName().substring(lastDot + 1);
        final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (mime != null) {
            return mime;
        }
    }
    return "application/octet-stream";
}
 
源代码21 项目: android_9.0.0_r45   文件: DocumentsProvider.java
/**
 * Return concrete MIME type of the requested document. Must match the value
 * of {@link Document#COLUMN_MIME_TYPE} for this document. The default
 * implementation queries {@link #queryDocument(String, String[])}, so
 * providers may choose to override this as an optimization.
 * <p>
 * @throws AuthenticationRequiredException If authentication is required from
 *            the user (such as login credentials), but it is not guaranteed
 *            that the client will handle this properly.
 */
public String getDocumentType(String documentId) throws FileNotFoundException {
    final Cursor cursor = queryDocument(documentId, null);
    try {
        if (cursor.moveToFirst()) {
            return cursor.getString(cursor.getColumnIndexOrThrow(Document.COLUMN_MIME_TYPE));
        } else {
            return null;
        }
    } finally {
        IoUtils.closeQuietly(cursor);
    }
}
 
源代码22 项目: storage-samples   文件: MyCloudProvider.java
/**
 * Get a file's MIME type
 *
 * @param file the File object whose type we want
 * @return the MIME type of the file
 */
private static String getTypeForFile(File file) {
    if (file.isDirectory()) {
        return Document.MIME_TYPE_DIR;
    } else {
        return getTypeForName(file.getName());
    }
}
 
private Object[] getDocumentValues(
    String[] projection, DocumentMetadata metadata) {
  Object[] row = new Object[projection.length];
  for (int i = 0; i < projection.length; ++i) {
    switch (projection[i]) {
      case Document.COLUMN_DOCUMENT_ID:
        row[i] = toDocumentId(metadata.getUri());
        break;
      case Document.COLUMN_DISPLAY_NAME:
        row[i] = metadata.getDisplayName();
        break;
      case Document.COLUMN_FLAGS:
        // Always assume it can write to it until the file operation fails. Windows 10 also does
        // the same thing.
        int flag = metadata.canCreateDocument() ? Document.FLAG_DIR_SUPPORTS_CREATE : 0;
        flag |= Document.FLAG_SUPPORTS_WRITE;
        flag |= Document.FLAG_SUPPORTS_DELETE;
        flag |= Document.FLAG_SUPPORTS_RENAME;
        flag |= Document.FLAG_SUPPORTS_REMOVE;
        flag |= Document.FLAG_SUPPORTS_MOVE;
        row[i] = flag;
        break;
      case Document.COLUMN_MIME_TYPE:
        row[i] = metadata.getMimeType();
        break;
      case Document.COLUMN_SIZE:
        row[i] = metadata.getSize();
        break;
      case Document.COLUMN_LAST_MODIFIED:
        row[i] = metadata.getLastModified();
        break;
      case Document.COLUMN_ICON:
        row[i] = metadata.getIconResourceId();
        break;
    }
  }
  return row;
}
 
public String getMimeType() {
  switch (mEntry.getType()) {
    case DirectoryEntry.FILE_SHARE:
    case DirectoryEntry.WORKGROUP:
    case DirectoryEntry.SERVER:
    case DirectoryEntry.DIR:
      return Document.MIME_TYPE_DIR;

    case DirectoryEntry.LINK:
    case DirectoryEntry.COMMS_SHARE:
    case DirectoryEntry.IPC_SHARE:
    case DirectoryEntry.PRINTER_SHARE:
      throw new UnsupportedOperationException(
          "Unsupported type of Samba directory entry " + mEntry.getType());

    case DirectoryEntry.FILE:
      final String ext = getExtension(mEntry.getName());
      if (ext == null) {
        return GENERIC_MIME_TYPE;
      }

      final String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
      return (mimeType == null) ? GENERIC_MIME_TYPE : mimeType;
  }

  throw new IllegalStateException("Should never reach here.");
}
 
private void addExperimentToRow(MatrixCursor.RowBuilder row, ExperimentOverviewPojo overview) {
  row.add(
      Document.COLUMN_DISPLAY_NAME,
      Experiment.getDisplayTitle(getContext(), overview.getTitle()));
  row.add(Document.COLUMN_MIME_TYPE, Document.MIME_TYPE_DIR);
  row.add(Document.COLUMN_LAST_MODIFIED, overview.getLastUsedTimeMs());
  row.add(Document.COLUMN_SIZE, null);
}
 
/**
 * 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();
    }

}
 
源代码27 项目: android-StorageProvider   文件: MyCloudProvider.java
/**
 * Get a file's MIME type
 *
 * @param file the File object whose type we want
 * @return the MIME type of the file
 */
private static String getTypeForFile(File file) {
    if (file.isDirectory()) {
        return Document.MIME_TYPE_DIR;
    } else {
        return getTypeForName(file.getName());
    }
}
 
源代码28 项目: android_9.0.0_r45   文件: FileUtils.java
/**
 * Splits file name into base name and extension.
 * If the display name doesn't have an extension that matches the requested MIME type, the
 * extension is regarded as a part of filename and default extension for that MIME type is
 * appended.
 */
public static String[] splitFileName(String mimeType, String displayName) {
    String name;
    String ext;

    if (Document.MIME_TYPE_DIR.equals(mimeType)) {
        name = displayName;
        ext = null;
    } else {
        String mimeTypeFromExt;

        // Extract requested extension from display name
        final int lastDot = displayName.lastIndexOf('.');
        if (lastDot >= 0) {
            name = displayName.substring(0, lastDot);
            ext = displayName.substring(lastDot + 1);
            mimeTypeFromExt = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
                    ext.toLowerCase());
        } else {
            name = displayName;
            ext = null;
            mimeTypeFromExt = null;
        }

        if (mimeTypeFromExt == null) {
            mimeTypeFromExt = "application/octet-stream";
        }

        final String extFromMimeType = MimeTypeMap.getSingleton().getExtensionFromMimeType(
                mimeType);
        if (Objects.equals(mimeType, mimeTypeFromExt) || Objects.equals(ext, extFromMimeType)) {
            // Extension maps back to requested MIME type; allow it
        } else {
            // No match; insist that create file matches requested MIME
            name = displayName;
            ext = extFromMimeType;
        }
    }

    if (ext == null) {
        ext = "";
    }

    return new String[] { name, ext };
}
 
源代码29 项目: storage-samples   文件: MyCloudProvider.java
/**
 * Add a representation of a file to a cursor.
 *
 * @param result the cursor to modify
 * @param docId  the document ID representing the desired file (may be null if given file)
 * @param file   the File object representing the desired file (may be null if given docID)
 * @throws java.io.FileNotFoundException
 */
private void includeFile(MatrixCursor result, String docId, File file)
        throws FileNotFoundException {
    if (docId == null) {
        docId = getDocIdForFile(file);
    } else {
        file = getFileForDocId(docId);
    }

    int flags = 0;

    if (file.isDirectory()) {
        // Request the folder to lay out as a grid rather than a list. This also allows a larger
        // thumbnail to be displayed for each image.
        //            flags |= Document.FLAG_DIR_PREFERS_GRID;

        // Add FLAG_DIR_SUPPORTS_CREATE if the file is a writable directory.
        if (file.isDirectory() && file.canWrite()) {
            flags |= Document.FLAG_DIR_SUPPORTS_CREATE;
        }
    } else if (file.canWrite()) {
        // If the file is writable set FLAG_SUPPORTS_WRITE and
        // FLAG_SUPPORTS_DELETE
        flags |= Document.FLAG_SUPPORTS_WRITE;
        flags |= Document.FLAG_SUPPORTS_DELETE;
    }

    final String displayName = file.getName();
    final String mimeType = getTypeForFile(file);

    if (mimeType.startsWith("image/")) {
        // Allow the image to be represented by a thumbnail rather than an icon
        flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
    }

    final MatrixCursor.RowBuilder row = result.newRow();
    row.add(Document.COLUMN_DOCUMENT_ID, docId);
    row.add(Document.COLUMN_DISPLAY_NAME, displayName);
    row.add(Document.COLUMN_SIZE, file.length());
    row.add(Document.COLUMN_MIME_TYPE, mimeType);
    row.add(Document.COLUMN_LAST_MODIFIED, file.lastModified());
    row.add(Document.COLUMN_FLAGS, flags);

    // Add a custom icon
    row.add(Document.COLUMN_ICON, R.drawable.ic_launcher);
}
 
源代码30 项目: microMathematics   文件: AdapterDocuments.java
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;
}
 
 类所在包
 同包方法